Assuming I've the following list of lists:
dict1 = [['Jeremy', 25, 120000], ['Paul', 23, 75000], ['Mike', 32, 80000]]
I can easily sort the lists on index 2 as follows:
from operator import itemgetter
sorted_dict = sorted(dict1, key=itemgetter(1))
print(sorted_dict)
>>>
[['Paul', 23, 75000], ['Jeremy', 25, 120000], ['Mike', 32, 80000]]
Things get a little more complicated with a dictionary of lists. Assuming I've the following:
dict2 = {'employee1':['Paul', 23, 75000],
'employee2':['Mike', 32, 80000],
'employee3':['Jeremy', 25, 120000]}
I can approximate a sort on index 2 as follows:
from operator import itemgetter
#First, extract lists
result1 = dict2.values()
#Second, sort list by index 2
result2 = sorted(result1, key=itemgetter(1))
#Finally, use for loop to sort dictionary
for a in result2:
for b in dict2.keys():
if a == dict2[b]:
print("{0}:{1}".format(b,a))
>>>
employee1:['Paul', 23, 75000]
employee3:['Jeremy', 25, 120000]
employee2:['Mike', 32, 80000]
I would like a better way to perform a sort on the dictionary. I found a community wiki post on sorting dictionaries (here) but the dictionary in that post has constant key. In my dictionary above, each list has a unique key.
Thanks.
Using Python 3.4.1
First, you can't sort a dict
because it doesn't have order 1. You can sort it's items however:
sorted(d.items(), key=lambda t: t[1][1])
Should do the trick.
notes
t[1] # => the "value", t[0] is the "key"
t[1][1] # => The second element in the value.
You'll get a list of 2-tuples (where the first element is the key, and the second element is the value). Of course, you could always pass this list of 2-tuples directly to collections.OrderedDict
if you wanted to construct a dict which has an ordering after the fact ...
1More correctly, the order is arbitrary and could change based on key insertions, deletions, python implementation or version ... (which is why it's easier to say that dict's are unordered)
You can sort the keys and create another dict with ordered keys
>>>>from collections import OrderedDict
>>>>dict2 = {'employee1':['Paul', 23, 75000],
'employee2':['Mike', 32, 80000],
'employee3':['Jeremy', 25, 120000]}
>>>>OrderedDict([
#new item: ordered key, it's value form original dict
( x, dict2[x])
#sort keys of original dict
for x in sorted(dict2, key=lambda x: dict2[x][1])
])
OrderedDict([('employee1', ['Paul', 23, 75000]), ('employee3', ['Jeremy', 25, 120000]), ('employee2', ['Mike', 32, 80000])])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With