
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
# 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
# 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
# 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
# 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
# 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
# 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
- Related Articles
- How to sort a list of strings in Python?
- Python program to Sort a List of Strings by the Number of Unique Characters
- Python – Sort by Rear Character in Strings List
- Python – Sort given list of strings by numeric part of string
- How to remove empty strings from a list of strings in Python?
- How to sort a Python date string list?
- Python – Sort given list of strings by part the numeric part of string
- Python – To convert a list of strings with a delimiter to a list of tuple
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Python program to sort a list of tuples alphabetically
- How to sort the objects in a list in Python?
- Python program to convert a list of strings with a delimiter to a list of tuple
- How to sort strings in JavaScript?
- Python program to Sort Strings by Punctuation count
