john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

sort sorted list of dictionaries itemgetter

sorted( "This is a test string".split(), key=str.lower)     # ['a', 'is', 'string', 'test', 'This']
# key is called once on each list element prior to making comparisons


import operator

list_of_dicts.sort( key=operator.itemgetter( 'name' ) ) # sort is in place sorting so slightly faster than sorted() which makes a copy

newlist = sorted( original, key=itemgetter( 'name' ), reverse=True )    # reversed , or of course .reverse()

  • « concurrency threaded multiprocess requests upload
  • oauth signature library digital signature hmac »

Published

May 17, 2013

Category

python

~54 words

Tags

  • dictionaries 1
  • itemgetter 1
  • list 23
  • of 13
  • python 180
  • sort 11
  • sorted 2