<p>What is the best way of creating an alphabetically sorted list in Python?</p>

[{'answer_id': 36143, 'author': 'rix0rrr', 'author_id': 2474, 'author_profile': 'https://Stackoverflow.com/users/2474', 'pm_score': 5, 'selected': False, 'text': '<pre><code>list.sort()\n</code></pre>\n\n<p>It really is that simple :)</p>\n'}, {'answer_id': 36156, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 10, 'selected': True, 'text': '<p>Basic answer:</p>\n\n<pre><code>mylist = ["b", "C", "A"]\nmylist.sort()\n</code></pre>\n\n<p>This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the <a href="http://docs.python.org/library/functions.html#sorted" rel="noreferrer"><code>sorted()</code></a> function:</p>\n\n<pre><code>for x in sorted(mylist):\n    print x\n</code></pre>\n\n<p>However, the examples above are a bit naive, because they don\'t take locale into account, and perform a case-sensitive sorting. You can take advantage of the optional parameter <code>key</code> to specify custom sorting order (the alternative, using <code>cmp</code>, is a deprecated solution, as it has to be evaluated multiple times - <code>key</code> is only computed once per element).</p>\n\n<p>So, to sort according to the current locale, taking language-specific rules into account (<a href="http://docs.python.org/library/functools.html#functools.cmp_to_key" rel="noreferrer"><code>cmp_to_key</code></a> is a helper function from functools):</p>\n\n<pre><code>sorted(mylist, key=cmp_to_key(locale.strcoll))\n</code></pre>\n\n<p>And finally, if you need, you can specify a <a href="http://docs.python.org/library/locale.html" rel="noreferrer">custom locale</a> for sorting:</p>\n\n<pre><code>import locale\nlocale.setlocale(locale.LC_ALL, \'en_US.UTF-8\') # vary depending on your lang/locale\nassert sorted((u\'Ab\', u\'ad\', u\'aa\'),\n  key=cmp_to_key(locale.strcoll)) == [u\'aa\', u\'Ab\', u\'ad\']\n</code></pre>\n\n<p>Last note: you will see examples of case-insensitive sorting which use the <code>lower()</code> method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:</p>\n\n<pre><code># this is incorrect!\nmylist.sort(key=lambda x: x.lower())\n# alternative notation, a bit faster, but still wrong\nmylist.sort(key=str.lower)\n</code></pre>\n'}, {'answer_id': 36220, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>But how does this handle language specific sorting rules? Does it take locale into account?</p>\n</blockquote>\n\n<p>No, <code>list.sort()</code> is a generic sorting function. If you want to sort according to the Unicode rules, you\'ll have to define a custom sort key function. You can try using the <a href="http://jtauber.com/blog/2006/01/27/python_unicode_collation_algorithm/" rel="noreferrer">pyuca</a> module, but I don\'t know how complete it is.</p>\n'}, {'answer_id': 36395, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 6, 'selected': False, 'text': '<p>It is also worth noting the <a href="https://docs.python.org/3/library/functions.html#sorted" rel="noreferrer" title="sorted"><code>sorted()</code></a> function:</p>\n\n<pre><code>for x in sorted(list):\n    print x\n</code></pre>\n\n<p>This returns a new, sorted version of a list without changing the original list.</p>\n'}, {'answer_id': 1640634, 'author': 'schmichael', 'author_id': 10275, 'author_profile': 'https://Stackoverflow.com/users/10275', 'pm_score': 4, 'selected': False, 'text': "<p>The proper way to sort strings is:</p>\n\n<pre><code>import locale\nlocale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale\nassert sorted((u'Ab', u'ad', u'aa'), cmp=locale.strcoll) == [u'aa', u'Ab', u'ad']\n\n# Without using locale.strcoll you get:\nassert sorted((u'Ab', u'ad', u'aa')) == [u'Ab', u'aa', u'ad']\n</code></pre>\n\n<p>The previous example of <code>mylist.sort(key=lambda x: x.lower())</code> will work fine for ASCII-only contexts.</p>\n"}, {'answer_id': 43930555, 'author': 'JON', 'author_id': 5289655, 'author_profile': 'https://Stackoverflow.com/users/5289655', 'pm_score': 0, 'selected': False, 'text': '<p>Suppose <code>s = "ZWzaAd"</code> </p>\n\n<p>To sort above string the simple solution will be below one.</p>\n\n<pre><code>print \'\'.join(sorted(s))\n</code></pre>\n'}, {'answer_id': 47993007, 'author': 'Mahmud Ahsan', 'author_id': 339119, 'author_profile': 'https://Stackoverflow.com/users/339119', 'pm_score': 4, 'selected': False, 'text': '<p>Please use sorted() function in Python3</p>\n\n<pre><code>items = ["love", "like", "play", "cool", "my"]\nsorted(items2)\n</code></pre>\n'}, {'answer_id': 51826113, 'author': 'Dragos Alexe', 'author_id': 5379112, 'author_profile': 'https://Stackoverflow.com/users/5379112', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Or maybe:</p>\n</blockquote>\n\n<pre><code>names = [\'Jasmine\', \'Alberto\', \'Ross\', \'dig-dog\']\nprint ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))\n</code></pre>\n'}, {'answer_id': 57692644, 'author': 'vlz', 'author_id': 1879728, 'author_profile': 'https://Stackoverflow.com/users/1879728', 'pm_score': 1, 'selected': False, 'text': '<p>Old question, but if you want to do <strong>locale-aware sorting without setting</strong> <code>locale.LC_ALL</code> you can do so by using the <a href="http://pypi.python.org/pypi/PyICU" rel="nofollow noreferrer">PyICU library</a> as suggested by <a href="https://stackoverflow.com/a/11124645/1879728">this answer</a>:</p>\n\n<pre class="lang-py prettyprint-override"><code>import icu # PyICU\n\ndef sorted_strings(strings, locale=None):\n    if locale is None:\n       return sorted(strings)\n    collator = icu.Collator.createInstance(icu.Locale(locale))\n    return sorted(strings, key=collator.getSortKey)\n</code></pre>\n\n<p>Then call with e.g.:</p>\n\n<pre class="lang-py prettyprint-override"><code>new_list = sorted_strings(list_of_strings, "de_DE.utf8")\n</code></pre>\n\n<p>This worked for me without installing any locales or changing other system settings.</p>\n\n<p>(This was already suggested <a href="https://stackoverflow.com/questions/36139/how-to-sort-a-list-of-strings#comment62733021_36156">in a comment above</a>, but I wanted to give it more prominence, because I missed it myself at first.)</p>\n'}, {'answer_id': 59223861, 'author': 'asing177', 'author_id': 5567627, 'author_profile': 'https://Stackoverflow.com/users/5567627', 'pm_score': 1, 'selected': False, 'text': "<pre><code>l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']\nl.sort()\nprint(l1)\n</code></pre>\n\n<p>Result </p>\n\n<blockquote>\n  <p>['abc', 'ba', 'cd', 'dc', 'xy']</p>\n</blockquote>\n"}, {'answer_id': 62164160, 'author': 'Hedayatullah Sarwary', 'author_id': 6119631, 'author_profile': 'https://Stackoverflow.com/users/6119631', 'pm_score': 0, 'selected': False, 'text': '<p>It is simple:\n<a href="https://trinket.io/library/trinkets/5db81676e4" rel="nofollow noreferrer">https://trinket.io/library/trinkets/5db81676e4</a></p>\n\n<pre><code>scores = \'54 - Alice,35 - Bob,27 - Carol,27 - Chuck,05 - Craig,30 - Dan,27 - Erin,77 - Eve,14 - Fay,20 - Frank,48 - Grace,61 - Heidi,03 - Judy,28 - Mallory,05 - Olivia,44 - Oscar,34 - Peggy,30 - Sybil,82 - Trent,75 - Trudy,92 - Victor,37 - Walter\'\n</code></pre>\n\n<p>scores = scores.split(\',\')\nfor x in sorted(scores):\n  print(x)</p>\n'}]
['https://Stackoverflow.com/questions/36139', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3205/']