In dictionaries or list, could be selected how is the order of sorted() or similar order method?

This question comes from [this exercise]( Learn Python 3 | Codecademy), and one of [the forum related question](Does the dictionary keys() function return the keys in any specific order? - FAQ / Python FAQ - Codecademy Forums) which leads to this.

well, according to the forum answers, i extracted that if we use the sorted() method together with the method keys() method (users = sorted(user_ids.keys())) we get back the result ordered by alphabet. And, starting from Python 3.7, is documented that if we use the list(dict) to transform the dictionary into a list we have the dictionary ordered by insertion.

My question is. It exists any parameter that allows us to modify the order the values are being returned? Something like Reversed == True. if not, a quick way to do it? i was thinking in a regex expression or something like that.

For example, if i want to get back the dictionary but instead of alphabetical order of the keys, by ordinal order of values in case they are numbers or by alphabetical order in the values. In other words, if it is possible to modify the default behaviour of sorted(), list() or similar method regarding order.

I have been searching for a while in internet and couldn’t figure out.

More especifically, in code

user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384}

user_ids["pythonGuy"] = 123
users = sorted(user_ids.keys())
list_user_ids = list(user_ids)
users_b =[*user_ids.keys()]


I guess you could use the key argument with sorted().
for example, to get the keys (into a list) sorted by the values for each key of the dictionary:
sorted_list = sorted(user_ids, key = user_ids.get)


user_ids = {"teraCoder": 100019, "pythonGuy": 182921, "samTheJavaMaam": 123112, "lyleLoop": 102931, "keysmithKeith": 129384} sorted_list = sorted(user_ids, key = user_ids.get) print(sorted_list) print([user_ids[key] for key in sorted_list])
1 Like

yes. exactly what i was looking for! :smiley:

Thank you for answering and take some time. Could you explain the artifact by using the key = user_ids.get. I tried to print the user_ids.get to check what is happening and i just got the location in memory of the… dictionary?. i don’t see the exact relation with the loop and especifically how it impacts to order by value. With that method you don’t get the key:value pairs instead of just the value?

I was checking a link from sorted() methods that gives examples of how to use. Sorting HOW TO — Python 3.10.4 documentation. Anyway, i still don’t get the trick of the dict.get trick to the values xd

Both list.sort() and sorted() have a key parameter to specify a function (or other callable) to be called on each list element prior to making comparisons.

The value of the key parameter should be a function (or other callable) that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record.

for a dictionary the .get method returns the value for a key …
dict1 = {'a': 3, 'b': 4, 'c': 1}
you can do
a = dict1['a']
a = dict1.get('a')
to accomplish the same thing
(.get is useful because it doesn’t give you an error when the key doesn’t exist.)

sorted(dict1) returns a list,
the key parameter/argument is used to apply a function when checking how to sort it …
so it would sort it by the result of applying that function to each item/key.

dict1.get is a function that can be applied to each item, so it can be used for this key argument.
sorted1 = sorted(dict1, key = dict1.get)

1 Like

Thank you for the explanation :smiley: