Sorting lists permanently

To sort the list alphabetically and also keep the sorted order of elements, the sort() method is used:

last_names = ['Jones', 'Antunovich', 'Daniels', 'Thomson']
last_names.sort()

print(last_names)

>>>
['Antunovich', 'Daniels', 'Jones', 'Thomson']
>>>

The order of elements is permanently modified:

print(last_names[0])

>>>
Antunovich
>>>

To do a reverse sort, the reverse=True parameter can be specified:

last_names.sort(reverse=True)

print(last_names)
print(last_names[0])

['Thomson', 'Jones', 'Daniels', 'Antunovich']
Thomson
Geek University 2022