In: Computer Science
Write a python programA3) Lambda Expressions. Use lambda expression to sort the list ["democratic", "republican", "equal", "python", "break", "two", "ruby"] by: a) the last 2 characters of each list item in reverse order b) create new list from list above where each item has only 3 characters and first one is capitalized. For example, the list item = "democratic" will transform into "Dem". Apply alphabetically sorting to the new list.
Program Code Screenshot
Sample Output
Program Code to Copy
l = ["democratic", "republican", "equal", "python", "break",
"two", "ruby"]
#Sort the list by last 2 chars
l.sort(key=lambda x : x[-2:],reverse=True)
print('After sorting by last 2 chars ')
print(l)
#Create a new list by taking first 3 chars
nl = [x[0].upper()+x[1:3] for x in l]
print('New list by taking first 3 chars ',nl)
nl.sort()
print('After sorting alphabetically : ')
print(nl)