How to sort a list of strings

0 votes

What is the best way of creating an alphabetically sorted list in Python?

Jun 4, 2018 in Python by charlie_brown
• 7,720 points
1,491 views

1 answer to this question.

0 votes

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_keyis 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)
answered Jun 4, 2018 by aryya
• 7,460 points

Related Questions In Python

0 votes
1 answer

How to sort a list of strings?

Try  items = ["live", "like", "code", "cool", "bug"] ...READ MORE

answered Jul 27, 2018 in Python by Priyaj
• 58,020 points
840 views
+1 vote
4 answers

how to sort a list of numbers without using built-in functions like min, max or any other function?

Yes it is possible. You can refer ...READ MORE

answered Jun 27, 2019 in Python by Arvind
• 3,050 points
188,415 views
0 votes
1 answer

How to perform string operations on every element of a list of strings?

This can be done using list comprehension ...READ MORE

answered Jul 18, 2019 in Python by Arvind
• 3,050 points
1,759 views
0 votes
1 answer

What is the recommended way to randomize a list of strings using Python?

Hi. Nice question. Here is the simplified answer ...READ MORE

answered Jan 18, 2019 in Python by Nymeria
• 3,560 points
922 views
0 votes
1 answer

How to zip with a list output in Python instead of a tuple output?

Good question - Considering that you are ...READ MORE

answered Feb 7, 2019 in Python by Nymeria
• 3,560 points
1,756 views
0 votes
1 answer

how to convert a group of strings to integer?

a = ['1','2','3','4'] b = list(map(int,a)) print(b) this is how ...READ MORE

answered Mar 18, 2019 in Python by Mohammad
• 3,230 points
1,075 views
0 votes
1 answer

How to count the number of elements in a list?

To count the number of elements of ...READ MORE

answered May 27, 2019 in Python by Nisa
• 1,090 points
5,237 views
0 votes
1 answer

How to get size of a list?

Hi, You can use built-in function "len" to ...READ MORE

answered Jun 21, 2019 in Python by Shabnam
• 930 points
722 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,460 points
1,370 views
0 votes
1 answer

How to extract or split characters from number strings using Pandas?

You could just simply use a conversion ...READ MORE

answered Sep 18, 2018 in Python by aryya
• 7,460 points
2,053 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP