What is the best way of creating an alphabetically sorted list in Python?
11 Answers
Basic answer:
mylist = ["b", "C", "A"]
mylist.sort()
This modifies your original list (i.e. sorts in-place). To get a sorted copy of the list, without changing the original, use the sorted() function:
for x in sorted(mylist):
    print x
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 key to specify custom sorting order (the alternative, using cmp, is a deprecated solution, as it has to be evaluated multiple times - key is only computed once per element).
So, to sort according to the current locale, taking language-specific rules into account (cmp_to_key is a helper function from functools):
sorted(mylist, key=cmp_to_key(locale.strcoll))
And finally, if you need, you can specify a custom locale for sorting:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'),
  key=cmp_to_key(locale.strcoll)) == [u'aa', u'Ab', u'ad']
Last note: you will see examples of case-insensitive sorting which use the lower() method - those are incorrect, because they work only for the ASCII subset of characters. Those two are wrong for any non-English data:
# this is incorrect!
mylist.sort(key=lambda x: x.lower())
# alternative notation, a bit faster, but still wrong
mylist.sort(key=str.lower)
- 31,963
 - 27
 - 115
 - 151
 
- 186,300
 - 67
 - 213
 - 256
 
- 
                    42
 - 
                    2Good point. I'll leave my current example as-is, since it's probably easier for a beginner to see what's happening, but I'll keep that in mind in the future. – Eli Courtwright Oct 28 '08 at 18:48
 - 
                    1If anyone is curious, performance of list.sort() can be found [here](http://stackoverflow.com/questions/1517347/about-pythons-built-in-sort-method) – Hari Ganesan Feb 04 '14 at 17:26
 - 
                    How does this `mylist.sort(key=str.lower)` work? What is the name of this function passing construct? – lisak Aug 16 '15 at 13:26
 - 
                    @J.F.Sebastian `str.lower` will not sort correctly for non-ASCII characters, let this example: `['a', 'b', 'â']` – fikr4n Jun 04 '16 at 00:20
 - 
                    1@BornToCode : 1- [I know](http://ru.stackoverflow.com/a/446833). Look at the revision (2008) my comment replies to (my comment is about the unnecessary use of lambda). 2- sorting non-ASCII characters is a big separate topic. [PyICU could be used](http://stackoverflow.com/a/11124645) instead of the locale-based solution. – jfs Jun 04 '16 at 00:48
 - 
                    I can't believe it but it doesn't work: `print([1, 2, 3].sort())` returns `None`! – Dmitry Oct 28 '17 at 21:00
 - 
                    1@Dmitry This is because you are printing the return value of the sort function called in `[1, 2, 3].sort()`. As `sort()` sorts the list in place (ie, changes the list directly), it doesn't return the sorted list, and actually doesn't return anything, so your print statement prints `None`. If you saved your list to a variable, say `x`, called `x.sort()`, then `print(x)`, you would see the sorted list. – bjg222 Dec 30 '17 at 20:19
 
It is also worth noting the sorted() function:
for x in sorted(list):
    print x
This returns a new, sorted version of a list without changing the original list.
- 778
 - 6
 - 17
 
- 951,095
 - 183
 - 1,149
 - 1,285
 
list.sort()
It really is that simple :)
- 9,856
 - 5
 - 45
 - 48
 
- 
                    In general, it is surprisingly far from simple. But ok, the simplest case is actually simple. – Volker Siegel Aug 13 '23 at 16:52
 
The proper way to sort strings is:
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # vary depending on your lang/locale
assert sorted((u'Ab', u'ad', u'aa'), cmp=locale.strcoll) == [u'aa', u'Ab', u'ad']
# Without using locale.strcoll you get:
assert sorted((u'Ab', u'ad', u'aa')) == [u'Ab', u'aa', u'ad']
The previous example of mylist.sort(key=lambda x: x.lower()) will work fine for ASCII-only contexts.
- 412
 - 3
 - 8
 
Please use sorted() function in Python3
items = ["love", "like", "play", "cool", "my"]
sorted(items2)
- 1,755
 - 19
 - 18
 
But how does this handle language specific sorting rules? Does it take locale into account?
No, list.sort() 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 pyuca module, but I don't know how complete it is.
- 197,344
 - 39
 - 212
 - 226
 
l =['abc' , 'cd' , 'xy' , 'ba' , 'dc']
l.sort()
print(l)
Result
['abc', 'ba', 'cd', 'dc', 'xy']
- 3,277
 - 2
 - 24
 - 35
 
- 934
 - 2
 - 13
 - 34
 
Old question, but if you want to do locale-aware sorting without setting locale.LC_ALL you can do so by using the PyICU library as suggested by this answer:
import icu # PyICU
def sorted_strings(strings, locale=None):
    if locale is None:
       return sorted(strings)
    collator = icu.Collator.createInstance(icu.Locale(locale))
    return sorted(strings, key=collator.getSortKey)
Then call with e.g.:
new_list = sorted_strings(list_of_strings, "de_DE.utf8")
This worked for me without installing any locales or changing other system settings.
(This was already suggested in a comment above, but I wanted to give it more prominence, because I missed it myself at first.)
- 911
 - 1
 - 10
 - 18
 
Suppose s = "ZWzaAd" 
To sort above string the simple solution will be below one.
print ''.join(sorted(s))
Or maybe:
names = ['Jasmine', 'Alberto', 'Ross', 'dig-dog']
print ("The solution for this is about this names being sorted:",sorted(names, key=lambda name:name.lower()))
- 131
 - 2
 
It is simple: https://trinket.io/library/trinkets/5db81676e4
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'
scores = scores.split(',') for x in sorted(scores): print(x)
- 2,664
 - 3
 - 24
 - 38