Python How to sort a list of strings


In this tutorial, we are going to see how to sort a list of strings. We'll sort the given list of strings with sort method and sorted function. And then we'll see how to sort the list of strings based on different criteria like length, value, etc.,

Let's see how to sort a list of strings using list.sort() method. The sort method list is an insort. It'll directly sort the original list. Let's see the code.

Example

 Live Demo

# list of strings
strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring']
# sorting the list in ascending order
strings.sort()
# printing the sorted list
print(strings)

Output

If you execute the above program, then you will get the following result.

['C', 'Django', 'Java', 'Javascript', 'Python', 'React', 'Spring']

We can sort the list in descending order with the argument reverse as True to the sort Let's see the code to sort the list in descending order.

Example

 Live Demo

# list of strings
strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring']
# sorting the list in descending order
strings.sort(reverse=True)
# printing the sorted list
print(strings)

Output

If you execute the above program, then you will get the following result.

['Spring', 'React', 'Python', 'Javascript', 'Java', 'Django', 'C']

We have seen how to sort the list of strings using sort method. Let's see how to sort the list of strings using sorted built-in function.

Example

 Live Demo

# list of strings
strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring']
# sorting the list in ascending order
sorted_strings = sorted(strings)
# printing the sorted list
print(sorted_strings)

Output

If you execute the above program, then you will get the following result.

['C', 'Django', 'Java', 'Javascript', 'Python', 'React', 'Spring']

We can also sort the list in descending order using sorted function by passing reverse as True to the function as a second argument. Let's see the code.

Example

 Live Demo

# list of strings
strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring']
# sorting the list in descending order
sorted_strings = sorted(strings, reverse=True)
# printing the sorted list
print(sorted_strings)

Output

If you execute the above program, then you will get the following result.

['Spring', 'React', 'Python', 'Javascript', 'Java', 'Django', 'C']

What if we want to sort the list of strings based on the length? Yeah, we can sort based on length using the sort method and sorted function by passing a key as an argument.Let's see how to sort the list of strings based on their length.

Example

 Live Demo

# list of strings
strings = ['Python', 'C', 'Java', 'Javascript', 'React', 'Django', 'Spring']
# sorting the list in ascending order - length
strings.sort(key=len)
# printing the sorted list
print(strings)

Output

If you execute the above program, then you will get the following result.

['C', 'Java', 'React', 'Python', 'Django', 'Spring', 'Javascript']

We can pass any function to the key argument. The sort method will sort the list based on the return value of the function given to the key argument. The same thing will work in the sorted function as well.

Let's see an example using the sorted function by sorting the list of strings based on their value.

Example

 Live Demo

# list of strings
strings = ['7', '34', '3', '23', '454', '12', '9']
# sorting the list in ascending order - int value
sorted_strings = sorted(strings, key=int)
# printing the sorted list
print(sorted_strings)

Output

If you execute the above program, then you will get the following result.

['3', '7', '9', '12', '23', '34', '454']

Conclusion

We can pass key and reverse arguments at a time to the sort method and sorted function to achieve whatever we want. Try and explore them. If you have any doubts regarding the tutorial, mention them in the comment section

Updated on: 07-Jul-2020

500 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements