Reputation: 21
How can I sort the list present in the dictionary:
fourB={"James":[10,11,9]}
I will have multiple entries but I want to be able to sort the list of integers out for each one of them. How can I do that? Thanks!Any help will be appreciated. :)
Upvotes: 0
Views: 107
Reputation: 249153
for numbers in fourB.values():
numbers.sort()
The above is better than iterating over the keys()
followed by indexing into fourB
, because here you avoid the dict
lookup.
If you love one-liners, here's one:
map(list.sort, fourB.values())
But take note if there are many keys in the dict, as this will return a list of [None]*len(fourB.values())
which is immediately discarded--and that's not optimally efficient. I'd stick with the obvious loop version for this reason and also for readability.
Upvotes: 6
Reputation: 28858
You should iterate over the keys and sort their values:
for k in fourB.keys():
fourB[k].sort()
Upvotes: 2