{"id":693,"date":"2010-01-18T15:57:34","date_gmt":"2010-01-18T19:57:34","guid":{"rendered":"http:\/\/www.dr-chuck.com\/wordpress\/?p=693"},"modified":"2011-12-17T12:31:58","modified_gmt":"2011-12-17T16:31:58","slug":"fun-python-syntax-operator-itemgetter-sorted-and-lambda-functions-oh-my","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2010\/01\/fun-python-syntax-operator-itemgetter-sorted-and-lambda-functions-oh-my\/","title":{"rendered":"Fun Python Syntax &#8211; operator.itemgetter(), sorted(), and lambda functions &#8211; Oh MY!"},"content":{"rendered":"<p>This is perhaps the coolest idiom of Python I have seen yet.  Thanks<br \/>\nto Clint for stopping by my office for a Python versus Perl versus PHP smackdown that got me thinking.<\/p>\n<p>This is a great way to sort a list of tuples on the second key:<\/p>\n<pre>\n>>> import operator\n>>> a = [ (1,'z'), (2, 'x'), (3, 'y') ]\n>>> a.sort(key=operator.itemgetter(1))\n>>> a\n[(2, 'x'), (3, 'y'), (1, 'z')]\n>>>\n<\/pre>\n<p>\nPretty clever.  The method name could be a bit shorter :)<\/p>\n<p>\nHere is an operator free version using lamda<\/p>\n<pre>\n>>> a.sort(key=lambda x: x[1])\n>>> a\n[(2, 'x'), (3, 'y'), (1, 'z')]\n<\/pre>\n<p>\nAnd here is one that uses the second entry and the first entry:<\/p>\n<pre>\n>>> a.sort(key=lambda x: (x[1], x[0]) )\n>>> a\n[(2, 'x'), (3, 'y'), (1, 'z')]\n<\/pre>\n<p>\nAnd then the most super-duper fun one of all &#8211; sorting a dictionary:<\/p>\n<pre>\n>>> a = { \"a\": 9, \"b\" : 8, \"c\" : 7 }\n>>> b = sorted(a.items())   # Sort by key\n>>> b\n[('a', 9), ('b', 8), ('c', 7)]\n>>> >>> c = sorted(a.items(),key=lambda x: x[1])  # By value\n>>> c\n[('c', 7), ('b', 8), ('a', 9)]\n>>> c = sorted(a.items(),key=lambda x: x[1], reverse=True)  # By Value Backwards\n>>> c\n[('a', 9), ('b', 8), ('c', 7)]\n>>> d = sorted(a.items(),key=lambda x: (x[1], x[0]), reverse=True)  # Value then Key\n>>> d\n[('a', 9), ('b', 8), ('c', 7)]\n>>>\n<\/pre>\n<p>\nTime to go put this in my Python book in Chapter 10 (www.py4inf.com).  Too bad it did not make it into version 0.0.2 printed copies &#8211; but it will make it into the Chapter 10 lecture slides!<\/p>\n<p>\nMore fun tricks at: http:\/\/wiki.python.org\/moin\/HowTo\/Sorting<\/p>\n<p>\n","protected":false},"excerpt":{"rendered":"<p>This is perhaps the coolest idiom of Python I have seen yet. Thanks to Clint for stopping by my office for a Python versus Perl versus PHP smackdown that got me thinking. This is a great way to sort a list of tuples on the second key: >>> import operator >>> a = [ (1,&#8217;z&#8217;), [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-693","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":1,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":2800,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/693\/revisions\/2800"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}