Sorting lists in python: sorted() vs sort()
sorting - How does the key argument in python's sorted function work? - Stack Overflow
python - Sorting a set of values - Stack Overflow
Tip - Use Python Builtin Binary Search (Bisect) on Sorted List
Videos
Code: https://www.30secondsofcode.org/articles/s/python-sortedlist-vs-list-sort
The function you pass in to key is given each of the items that are being sorted, and returns a "key" that Python can sort by. So, if you want to sort a list of strings by the reverse of the string, you could do this:
list_of_strings.sort(key=lambda s: s[::-1])
This lets you specify the value each item is sorted by, without having to change the item. That way, you don't have to build a list of reversed strings, sort that, then reverse them back.
# DON'T do this
data = ['abc', 'def', 'ghi', 'jkl']
reversed_data = [s[::-1] for s in data]
reversed_data.sort()
data = [s[::-1] for s in reversed_data]
# Do this
data.sort(key=lambda s: s[::-1])
In your case, the code is sorting each item by the second item in the tuple, whereas normally it would initially sort by the first item in the tuple, then break ties with the second item.
>>> votes = {'Charlie': 20, 'Able': 10, 'Baker': 20, 'Dog': 15}
If we apply .items() on the votes dictionary above we get:
>>> votes_items=votes.items()
>>> votes_items
[('Charlie', 20), ('Baker', 20), ('Able', 10), ('Dog', 15)]
#a list of tuples, each tuple having two items indexed 0 and 1
For each tuple, the first index [0] are the strings ('Charlie','Able','Baker','Dog') and the second index [1] the integers (20,10,20,15).
print(sorted(votes.items(), key = lambda x: x[1])) instructs python to sort the items(tuples) in votes using the second index [1] of each tuple, the integers, as the basis of the sorting.
Python compares each integer from each tuple and returns a list that has ranked each tuple in ascending order (this can be reversed with the reverse=True argument) using each tuple's integer as the key to determine the tuple's rank,
Where there is a tie in the key, the items are ranked in the order they are originally in the dictionary. (so ('Charlie', 20) is before ('Baker', 20) because there is a 20==20 tie on the key but ('Charlie', 20) comes before ('Baker', 20) in the original votes dictionary).
The output then is:
[('Able', 10), ('Dog', 15), ('Charlie', 20), ('Baker', 20)]
I hope this makes it easier to understand.
From a comment:
I want to sort each set.
That's easy. For any set s (or anything else iterable), sorted(s) returns a list of the elements of s in sorted order:
>>> s = set(['0.000000000', '0.009518000', '10.277200999', '0.030810999', '0.018384000', '4.918560000'])
>>> sorted(s)
['0.000000000', '0.009518000', '0.018384000', '0.030810999', '10.277200999', '4.918560000']
Note that sorted is giving you a list, not a set. That's because the whole point of a set, both in mathematics and in almost every programming language,* is that it's not ordered: the sets {1, 2} and {2, 1} are the same set.
You probably don't really want to sort those elements as strings, but as numbers (so 4.918560000 will come before 10.277200999 rather than after).
The best solution is most likely to store the numbers as numbers rather than strings in the first place. But if not, you just need to use a key function:
>>> sorted(s, key=float)
['0.000000000', '0.009518000', '0.018384000', '0.030810999', '4.918560000', '10.277200999']
For more information, see the Sorting HOWTO in the official docs.
* See the comments for exceptions.
Another method is to explicitly convert the set into a list (using list() function) and sort the list.
s = {10, 39, 3}
s_list = list(s)
s_list.sort() # sort in-place
# [3, 10, 39]
Since Python 3.7, dicts keep insertion order. So if you want to order a set but still want to be able to do membership checks in constant time, you can create a dictionary from the sorted list using dict.fromkeys() (the values are None by default).
s = {10, 39, 3}
s_sorted = dict.fromkeys(sorted(s)) # ascending order
# {3: None, 10: None, 39: None}
s_sorted = dict.fromkeys(sorted(s, reverse=True)) # descending order
# {39: None, 10: None, 3: None}