Python Decorate Sort Undecorate Pattern

How do you srt a dict of items in one line?

Pythoneer » A functional task, I am sure

d = {4: 6, 12: 4, 14: 14, 18: 16, 32: 6,    33: 24, 40: 27, 41: 6, 44: 25, 106: 2}sorted(d.items(), key=itemgetter(1))

You can also do something similar for strings of text:

Python DSU sorting idiom » STRAY neuron

words = “This is a test string from Andrew.”.split()sorted(words, key=str.lower) [’a', ‘Andrew.’, ‘from’, ‘is’, ’string’, ‘test’, ‘This’]

Leave a Reply

You must be logged in to post a comment.