Skip to content

Instantly share code, notes, and snippets.

@jaapz
Last active August 29, 2015 14:11
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 jaapz/aa8456a5f17ed9bb4434 to your computer and use it in GitHub Desktop.
Save jaapz/aa8456a5f17ed9bb4434 to your computer and use it in GitHub Desktop.
sorting a list using itemgetter
from operator import itemgetter
mylist = [
["Some", "Data", "30.0", 12.10],
["Foo", "Bar", "29.5", 90.00],
["Fizz", "Buzz", "29", 13.12]
]
# Sorting using a field that already has a nice sortable type (for example, a float).
my_sorted_list_4 = sorted(mylist, key=itemgetter(3))
# Sorting using a field that is a string, while we want to sort it as a float.
my_sorted_list_3 = sorted(mylist, key=lambda x: float(x[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment