python sort based on key code example
Example 1: python get dictionary keys sorted by value
sorted(A, key=A.get)
Example 2: python sort list in place
your_list.sort()
your_list = [42, 17, 23, 111]
your_list.sort()
print(your_list)
--> [17, 23, 42, 111]
your_list = ['42', '17', '23', '111']
your_list.sort(key=int)
print(your_list)
--> ['17', '23', '42', '111']
your_list =['cmd1','cmd10', 'cmd111', 'cmd50', 'cmd99']
your_list.sort(key=lambda x: int(x[3:]))
print(your_list)
--> ['cmd1', 'cmd10', 'cmd50', 'cmd99', 'cmd111']
your_list = [42, 17, 23, 111]
your_list_sorted = sorted(your_list)
print(your_list_sorted)
--> [17, 23, 42, 111]
Example 3: how to sort a list in python
l=[1,3,2,5]
l= sorted(l)
print(l)
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
Example 4: sorted python
var = sorted(old_var)