How to sort a list of strings in Python?


In Python, a list is a collection of values enclosed in square brackets. To sort a list in Python, we can use the sort() method or sorted() function. The sort() method sorts the list in place, modifying the original list. On the other hand, the sorted() function returns a new sorted list, leaving the original list unchanged. Both the sort() method and sorted() function sort the list in ascending order by default, but we can also sort the list in descending order by passing the reverse=True argument.

Here are three examples of how to sort a list of strings in Python:

Sort a list of strings in ascending order

Example

First, we define a list of strings called my_list. Then, we use the sort() method to sort the list in ascending order. Finally, we print the sorted list.

# Define a list of strings
my_list = ['apricot', 'mango', 'banana','orange']
# Sort the list in ascending order
my_list.sort()
# Print the sorted list
print(my_list)

Output

['apricot', 'banana', 'mango', 'orange']

Sort a list of strings in descending order

Example

Similar to the first example, we define a list of strings called my_list. However, this time we use the sort() method with the ‘reverse=True’ argument to sort the list in descending order. Finally, we print the sorted list.

# Define a list of strings
my_list = ['apricot', 'banana', 'mango', 'orange']
# Sort the list in descending order
my_list.sort(reverse=True)
# Print the sorted list
print(my_list)

Output

['orange', 'mango', 'banana', 'apricot']

Sort a list of strings using the sorted() function

Example

In this example, we again define a list of strings called my_list. However, this time we use the sorted() function to sort the list. The sorted() function returns a new sorted list and leaves the original list unchanged. We store the new sorted list in a variable called sorted_list. Finally, we print the sorted list.

# Define a list of strings
my_list = ['banana', 'mango', 'orange', 'apricot']
# Sort the list using the sorted() function
sorted_list = sorted(my_list)
# Print the sorted list
print(sorted_list)

Output

['apricot', 'banana', 'mango', 'orange']

Sort a list of strings based on string length

Example

In this example, we again define a list of strings called my_list. However, this time we use the sort() method with the key=len argument to sort the list based on string length. The len() function returns the length of each string in the list and this information is used to sort the list. Finally, we print the sorted list.

# Define a list of strings
my_list = ['mango','apricot','banana','orange']
# Sort the list based on string length
my_list.sort(key=len)
# Print the sorted list
print(my_list)

Output

['mango', 'banana', 'orange', 'apricot']

Sort a list of strings in a case-insensitive manner

Example

In this example, we define a list of strings called my_list. This list contains both upper-case and lower-case letters. We use the sort() method with the key=str.lower argument to sort the list in a case-insensitive manner. The str.lower() method is used to convert all the strings in the list to lower case before they are sorted. Finally, we print the sorted list.

# Define a list of strings
my_list = ['apricot', 'Orange', 'Banana', 'mango']
# Sort the list in a case-insensitive manner
my_list.sort(key=str.lower)
# Print the sorted list
print(my_list)

Output

['apricot', 'Banana', 'mango', 'Orange']

Sort a list of strings in reverse order based on the last character

Example

In this example, we again define a list of strings called my_list. We use the sort() method with the key=lambda x: x[-1] argument to sort the list based on the last character of each string. The lambda function is used to extract the last character of each string and this information is used to sort the list. Finally, we use the reverse=True argument to sort the list in reverse order based on the last character.

# Define a list of strings
my_list = ['apricot', 'banana', 'mango', 'orange']

# Sort the list in reverse order based on the last character
my_list.sort(key=lambda x: x[-1], reverse=True)

# Print the sorted list
print(my_list)

Output

['apricot', 'mango', 'orange', 'banana']

Updated on: 11-Aug-2023

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements