I'm guessing you want something like:
new_joe = sorted( joe,key = lambda x: x[1] )
which is the same thing as the slightly more efficient:
from operator import itemgetter
new_joe = sorted( joe, key = itemgetter(1) )
Or, you can sort joe
in place:
joe.sort(key=lambda x: x[1])
But, what if you want to have all of the 'miles'
lists together, and then sorted in ascending order after that?
joe.sort()
should do the trick ... (and now, hopefully I've covered enough bases that I hit the one you need).
Ahh, Now I see what you want. For this, you have a bunch of options. My favorite is a collections.defaultdict
:
from collections import defaultdict
d = defaultdict(list)
for k,v in joe:
d[k].append(v)
for k,v in d.items():
print(k,v)
But another options would be to sort joe
(e.g. joe.sort()
) and use itertools.groupby
.