6 ways to sort list of strings in Python

In this post, we are going to learn different 6 ways to sort list of strings in Python with code examples. In this post, we will sort the list of strings in ascending/descending order by using Sort() or sorted method.

We are going to use the following ways to sort list of strings in Python


  • sort list of strings alphabetically ascending order
  • sort list of stringsalphabetically descending order
  • Sort a List of strings by Length
  • sort list of strings by ignoring case
  • sort list of strings Numeric order ascending
  • sort list of strings Numeric order ascending

1. Sort list of string in alphabetically ascending order


In this example, we will be sorting the list in ascending order by using Sort() or sorted method. By default, both sort() and sorted() methods sort the list in ascending order.

Using Sort() Method

In this example, we are using the sort() method to sort a list of strings in alphabetic order

mylist = ['is','this','dog','a']
mylist.sort()
print(mylist)

Output

['a', 'dog', 'is', 'this']

Using Sorted() method

In this example, we are using the sorted() method to sort a list of strings in alphabetic order

mylist = ['is','this','dog','a']
sorted_list = sorted(mylist)
print(sorted_list)

Output

['a', 'dog', 'is', 'this']

2. Sort list of strings in alphabetically descending order


As we mentioned in the above example, by default the sort() function sort in ascending order. To sort a list in descending order we will have to use sort(reverse=True), which has an argument reverse set as true.

Python Program to Sort list of strings in alphabetically descending order

mylist = ['is','this','dog','a']
mylist.sort(reverse=True)
print(mylist)

Output

['this', 'is', 'dog', 'a']

3. Sort List of strings by Length


sort() and sorted() method take a optional key parameter.The key calls a function for each element of the list. We can pass sort( key=len). We can sort the list by string length.

list.sort( key=len)

Let us understand with example as below

mylist = ['is','this','Banana','apple','orange']
mylist.sort( key=len)
print(mylist)

Output

['is', 'this', 'apple', 'Banana', 'orange']

4. How to sort list of strings by ignoring case


When we have to sort a list of strings that contain case insensitive elements then the simple sort() and sorted() functions do not work. As we can see in the below example, the output is not sorted in order, We can overcome this problem by using additional arguments in the sort() & sorted() function.

mylist = ['this','Banana','apple','orange']
mylist.sort()
print(mylist)

Output

['Banana', 'apple', 'orange', 'this']

Syntax to pass parameter in sort() method

list.sort( key=function ) 

sort() and sorted() method take a optional key parameter.The key calls a function for each element of the list. We can pass list. sort( key=str.lower) to overcome the above problem. Let us understand the given examples below.

4.1 Sort List of strings ignoring case sensitive ascending order


We can pass key=str.lower to sort( key=str.lower) function to sort list of strings ignoring case sensitive ascending order

mylist = ['is','this','Banana','apple','orange']
mylist.sort( key=str.lower)
print(mylist)

Output

['apple', 'Banana', 'is', 'orange', 'this']

4.2 .Sort List of strings ignoring case descending order


We can pass reverse = True to list.sort(reverse=True, key=str.lower) to sort List of strings ignoring case sensitive descending order

mylist = ['is','this','Banana','apple','orange']
mylist.sort(reverse=True, key=str.lower)
print(mylist)

Output

['this', 'orange', 'is', 'Banana', 'apple']

5. Sort a list of string by Numeric order ascending


To sort a list of strings by passing int as key function in sort() function to sort a list of strings by numeric order. Let us understand with an example

mylist = ['500','100','800','300','50']
mylist.sort( key=int)
print(mylist)

Output

['50', '100', '300', '500', '800']

6. Sort a list of string by Numeric order Descending order


We can sort a list of strings by numeric order descending by passing int as key function in sort() function and reverse =True. Let us understand with an example

mylist = ['500','100','800','300','50']
mylist.sort(reverse=True, key=int)
print(mylist)

Output

['800', '500', '300', '100', '50']

Summary

We have explored different methods of how to sort list of strings in Python. Sort a list of strings in alphabetically ascending/descending order, sort a list of strings by ignoring case-sensitive in ascending/descending order, and many more.