Skip to content

Instantly share code, notes, and snippets.

@burakdede
Last active August 21, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burakdede/b303d8e44074f1b4f8cf991eae3d8d75 to your computer and use it in GitHub Desktop.
Save burakdede/b303d8e44074f1b4f8cf991eae3d8d75 to your computer and use it in GitHub Desktop.
positional itemgetter sort
"""
[('tr', 90), ('uk', 44), ('us', 1)]
[('us', 1), ('uk', 44), ('tr', 90)]
[('tr', 90), ('uk', 44), ('us', 1)]
"""
from operator import itemgetter
rows = [
("tr", 90),
("us", 1),
("uk", 44)
]
by_country_name = sorted(rows, key=itemgetter(0))
by_country_code = sorted(rows, key=itemgetter(1))
by_both = sorted(rows, key=itemgetter(0,1))
print(by_country_name)
print(by_country_code)
print(by_both)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment