<p>Dictionaries unlike lists are not ordered (and do not have the 'sort' attribute). Therefore, you can not rely on getting the items in the same order when first added. </p>

<p>What is the easiest way to loop through a dictionary containing strings as the key value and retrieving them in ascending order by key?</p>

<p>For example, you had this:</p>

<pre><code>d = {'b' : 'this is b', 'a': 'this is a' , 'c' : 'this is c'}
</code></pre>

<p>I want to print the associated values in the following sequence sorted by key:</p>

<pre><code>this is a
this is b
this is c
</code></pre>

[{'answer_id': 55188, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 2, 'selected': False, 'text': '<p>This snippet will do it. If you\'re going to do it frequently, you might want to make a \'sortkeys\' method to make it easier on the eyes.</p>\n<pre><code>keys = list(d.keys())\nkeys.sort()\nfor key in keys:\n    print d[key]\n</code></pre>\n<p>Edit: <a href="https://stackoverflow.com/a/55193/2745495">dF\'s solution</a> is better -- I forgot all about <code>sorted()</code>.</p>\n'}, {'answer_id': 55193, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 4, 'selected': False, 'text': '<p>Do you mean that you need the values sorted by the value of the key?\nIn that case, this should do it:</p>\n\n<pre><code>for key in sorted(d):\n    print d[key]\n</code></pre>\n\n<p><strong>EDIT:</strong> changed to use sorted(d) instead of sorted(d.keys()), thanks <a href="https://stackoverflow.com/users/1694/eli-courtwright">Eli</a>!</p>\n'}, {'answer_id': 55194, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 4, 'selected': False, 'text': '<p>Or shorter,</p>\n\n<pre><code>for key, value in sorted(d.items()):\n    print value\n</code></pre>\n'}, {'answer_id': 55197, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 0, 'selected': False, 'text': "<pre><code>&gt;&gt;&gt; d = {'b' : 'this is b', 'a': 'this is a' , 'c' : 'this is c'}\n&gt;&gt;&gt; for k,v in sorted(d.items()):\n...     print v, k\n... \nthis is a a\nthis is b b\nthis is c c\n</code></pre>\n"}, {'answer_id': 55202, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 0, 'selected': False, 'text': '<pre><code>d = {\'b\' : \'this is b\', \'a\': \'this is a\' , \'c\' : \'this is c\'}\nks = d.keys()\nks.sort()\nfor k in ks:\n    print "this is " + k\n</code></pre>\n'}, {'answer_id': 56134, 'author': 'Will Boyce', 'author_id': 5757, 'author_profile': 'https://Stackoverflow.com/users/5757', 'pm_score': 0, 'selected': False, 'text': '<pre><code>for key in sorted(d):\n  print d[key]\n</code></pre>\n'}, {'answer_id': 59235, 'author': 'Peter C', 'author_id': 1952, 'author_profile': 'https://Stackoverflow.com/users/1952', 'pm_score': 2, 'selected': False, 'text': '<p>You can also sort a dictionary by value and control the sort order:</p>\n\n<pre><code>import operator\n\nd = {\'b\' : \'this is 3\', \'a\': \'this is 2\' , \'c\' : \'this is 1\'}\n\nfor key, value in sorted(d.iteritems(), key=operator.itemgetter(1), reverse=True):\n    print key, " ", value\n</code></pre>\n\n<p>Output:<br>\nb   this is 3<br>\na   this is 2<br>\nc   this is 1</p>\n'}, {'answer_id': 40301443, 'author': 'Ukimiku', 'author_id': 6804704, 'author_profile': 'https://Stackoverflow.com/users/6804704', 'pm_score': 0, 'selected': False, 'text': '<p>Do you mean "sorted" instead of "ordered"? It seems your question aims at sorting a dictionary and not at ordering it. If you do mean "ordered", you can use an OrderedDict from the collections module. Those dictionaries remember the order in which the key/value pairs were entered:</p>\n\n<pre><code>from collections import OrderedDict\n</code></pre>\n\n<p>Reference information: <a href="https://docs.python.org/2/library/collections.html#collections.OrderedDict" rel="nofollow">https://docs.python.org/2/library/collections.html#collections.OrderedDict</a></p>\n'}]
['https://Stackoverflow.com/questions/55180', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4872/']