I am using Python 2.5. I have a dictionary with a list of lists as values
{'a': [['6', 3]], 'b': [['5', 1], ['6', 5]], 'c': [['446', 2],['6', 11],['67', 86]] }
I want to sort it by the 2nd element of the first list item, so, the above would be sorted like this:
'b': [['5', 1], ['6', 5]],
'c': [['446', 2],['6', 11],['67', 86]]
'a': [['6', 3]]
Any suggestions?
Thanks
Scott
A dictionary itself is unordered, so you can’t sort it per se. If you want to create a sorted list of the key-value pairs, you can do that:
iteritemsreturns an iterable of (key, value) pairs, sox[1][0][1]there means “take the second element of this pair (which is the value), and take the first element of that (which is the first list in the list of lists) and take the second element in that — in order words, the second element in the first list, which is what you want.