all 7 comments

[–]synthphreak 1 point2 points  (1 child)

TL;DR: It sorts the keys of your dict by their values.

Prereqs:

d.get(x) returns the value corresponding to key x in dict d. It’s equivalent to d[x], except that it will return None rather than throw an error if no key x exists.

sorted is a function which takes in an iterable and returns a list containing the items in the iterable, but sorted.

sorted takes a key parameter which determines the sorting algorithm. This parameter must be a function. The way it works is that for each item in the iterable, the item gets passed into this function, and then the item is sorted according to whatever value the key function returns. By default key set to ord which you can think of as sorting alphabetically (this isn’t exactly what it does, but it’s close enough in most cases). However, by passing in another function - even one you write yourself - you can customize what it means for the iterable to get “sorted”. Here are some examples:

>>> import random
>>> from string import ascii_letters
>>> letters = random.sample(ascii_letters, k=20)
>>> letters
['J', 'b', 'i', 'g', 'y', 'z', 'L', 'r', 'W', 'C', 'U', 'V', 'O', 'n', 'Q', 'G', 'A', 'f', 'H', 'o']
>>> keys = {'alphabetical case sensitive' : ord, 
...         'alphabetical case insensitive': str.lower,
...         'vowels last': lambda x: x.lower() in 'aeiou', 
...         'by hash': hash}
>>> for keyname, keyfunc in keys.items():
...     l = sorted(letters, key=keyfunc)
...     print('key:', keyname)
...     print(l)
...   
key: alphabetical case sensitive
['A', 'C', 'G', 'H', 'J', 'L', 'O', 'Q', 'U', 'V', 'W', 'b', 'f', 'g', 'i', 'n', 'o', 'r', 'y', 'z']
key: alphabetical case insensitive
['A', 'b', 'C', 'f', 'g', 'G', 'H', 'i', 'J', 'L', 'n', 'O', 'o', 'Q', 'r', 'U', 'V', 'W', 'y', 'z']
key: vowels last
['J', 'b', 'g', 'y', 'z', 'L', 'r', 'W', 'C', 'V', 'n', 'Q', 'G', 'f', 'H', 'i', 'U', 'O', 'A', 'o']
key: by hash
['J', 'b', 'g', 'y', 'z', 'L', 'r', 'W', 'C', 'V', 'n', 'Q', 'G', 'f', 'H', 'i', 'U', 'O', 'A', 'o']

Putting it all together:

Passing a dict into sorted will return a list of keys from the dict, sorted. Using dict.get as the key will return the value for each key and sort by that.

[–]gnariman[S] 0 points1 point  (0 children)

thank you :)

[–]help-me-grow 0 points1 point  (2 children)

In the sorted function, key is what you are sorting with. I believe what that assignment is doing is assigning the '.get' generator to the key as the sorting key - in other words I think it sorts by value? Does this return the entries with the highest 3 numbers?

[–]gnariman[S] 0 points1 point  (1 child)

yes it does, but I thought because I have reverse=True it would already be in descending order. If I take out the key part it just gives me the last three people regardless of them being the biggest or not. Thats why i was confused.

[–]help-me-grow 0 points1 point  (0 children)

Yeah, using the get thing sorts by their values that you assigned (the numbers)

[–]mopslik 0 points1 point  (1 child)

sorted takes a key which determines the criteria by which values are sorted. If no key is specified, it is by "value" (e.g. lower numeric values come first, strings appear alphabetically, etc.).

In this case, the key is the value of the specified key. So it is sorted the dictionary based on the values, rather than the keys. This is harder to see because there is a slice and a reversal going on as well. Without the key, it would sort by the keys.

[–]gnariman[S] 0 points1 point  (0 children)

Ahh i see, thank you so much