Created
January 19, 2014 19:08
-
-
Save pybokeh/8509486 to your computer and use it in GitHub Desktop.
Sorting Python Dictionaries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<center><h1>Sorting Python Dictionary Examples</h1></center>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h3>I know of 3 different ways to sort a dictionary:</h3>\n", | |
"\n", | |
"* using sort() method with lambda function (if I remember correctly, this is slowest)\n", | |
"* using sort() method using operator.itemgetter\n", | |
"* using an OrderedDict from collections.OrderedDict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"mydict = {'b':4,'a':2,'d':1,'c':3}" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Using sort() using lambda function:</h2>\n", | |
"\n", | |
"---\n", | |
"<h4>Sort by values:</h4>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list_of_tuples = sorted(mydict.items(), key=lambda x: x[1])\n", | |
"my_list_of_tuples" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 30, | |
"text": [ | |
"[('d', 1), ('a', 2), ('c', 3), ('b', 4)]" | |
] | |
} | |
], | |
"prompt_number": 30 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"Sort by keys:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list_of_tuples = sorted(mydict.items(), key=lambda x: x[0])\n", | |
"my_list_of_tuples" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 31, | |
"text": [ | |
"[('a', 2), ('b', 4), ('c', 3), ('d', 1)]" | |
] | |
} | |
], | |
"prompt_number": 31 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 4, | |
"metadata": {}, | |
"source": [ | |
"NOTE: In all 3 methods, you can use the \"reverse\" parameter to sort in descending order:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"my_list_of_tuples = sorted(mydict.items(), key=lambda x: x[1], reverse=True)\n", | |
"my_list_of_tuples" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 33, | |
"text": [ | |
"[('b', 4), ('c', 3), ('a', 2), ('d', 1)]" | |
] | |
} | |
], | |
"prompt_number": 33 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Using sort() using operator.itemgetter:</h2>\n", | |
"\n", | |
"---\n", | |
"<h4>Sorting by values</h4>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from operator import itemgetter\n", | |
"# Sort by value\n", | |
"sorted(mydict.items(), key=itemgetter(1))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 28, | |
"text": [ | |
"[('d', 1), ('a', 2), ('c', 3), ('b', 4)]" | |
] | |
} | |
], | |
"prompt_number": 28 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Sorting by keys</h4>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# Sort by key\n", | |
"sorted(mydict.items(), key=itemgetter(0))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 27, | |
"text": [ | |
"[('a', 2), ('b', 4), ('c', 3), ('d', 1)]" | |
] | |
} | |
], | |
"prompt_number": 27 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h2>Using OrderedDict from collections.OrderedDict:</h2>\n", | |
"\n", | |
"---\n", | |
"<h4>Sorting by values</h4>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from collections import OrderedDict\n", | |
"\n", | |
"d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}\n", | |
"OD = OrderedDict(sorted(d.items(), key=lambda t: t[1]))\n", | |
"OD" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 35, | |
"text": [ | |
"OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])" | |
] | |
} | |
], | |
"prompt_number": 35 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"<h4>Sorting by keys</h4>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"OD = OrderedDict(sorted(d.items(), key=lambda t: t[0]))\n", | |
"OD" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 36, | |
"text": [ | |
"OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])" | |
] | |
} | |
], | |
"prompt_number": 36 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 3, | |
"metadata": {}, | |
"source": [ | |
"BONUS: Prettyfied way of printing your Python dictionary:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"for key in OD.keys():\n", | |
" print(\"OD[\"+key+\"]:\",OD[key])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"OD[apple]: 4\n", | |
"OD[banana]: 3\n", | |
"OD[orange]: 2\n", | |
"OD[pear]: 1\n" | |
] | |
} | |
], | |
"prompt_number": 37 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment