1>>> the_list = [1,2,3]
2>>> reversed_list = the_list.reverse()
3>>> list(reversed_list)
4[3,2,1]
5
6OR
7
8>>> the_list = [1,2,3]
9>>> the_list[::-1]
10[3,2,1]
1my_list = [1, 2, 3]
2my_list.reverse() # my_list is modified
3print(my_list) # '[3, 2, 1]'
4my_revert = my_list[::-1] # my_list stays [3, 2, 1]
5print(my_revert) # '[1, 2, 3]'
6# Item by item reverse with range(<start>, <end>, <step>)
7for i in range(len(my_list), 0, -1):
8 print(my_list[i-1]) # '1' '2' '3'
9for i in reversed(range(len(my_list))):
10 print(my_list[i]) # '1' '2' '3'
1myList = [0,1,2,3,4,5]
2myList.reverse()
3print(myList)
4#OR
5print(myList[::-1])
1#1 Changes list
2list.sort(reverse=True)
3#2 Returns sorted list
4sorted(list, reverse=True)
1records = [{'spam': 1, 'foo': 'z'}, {'spam': 2, 'foo': 'a'}]
2sorted(records, key=lambda record: record['spam']) # default: ascending
3# ==> [{'spam': 1, 'foo': 'z'}, {'spam': 2, 'foo': 'a'}]
4sorted(records, key=lambda record: record['spam'], reverse=False) # ascending
5# ==> [{'spam': 1, 'foo': 'z'}, {'spam': 2, 'foo': 'a'}]
6sorted(records, key=lambda record: record['spam'], reverse=True) # descending
7# ==> [{'spam': 2, 'foo': 'a'}, {'spam': 1, 'foo': 'z'}]
8# -----------------------
9sorted(records, key=lambda dictionary: dictionary['foo']) # default: ascending
10# ==> [{'spam': 2, 'foo': 'a'}, {'spam': 1, 'foo': 'z'}]
11sorted(records,key=lambda alias: alias['foo'], reverse=True) # descending
12# ==> [{'spam': 1, 'foo': 'z'}, {'spam': 2, 'foo': 'a'}]
13# -----------------------
14print(records)
15# ==> [{'spam': 1, 'foo': 'z'}, {'spam': 2, 'foo': 'a'}] # NOT modified