How to sort a list of strings in Python
Last Updated : 19 Nov, 2024
In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().
Using sort() Method
The sort() method sorts a list in place and modifying the original list directly.
Python a = ["banana", "apple", "cherry"] # Sorting list in place a.sort() print(a)
Output['apple', 'banana', 'cherry']
Explanation:
- a.sort() sorts the original list by modifying it.
- Strings are sorted lexicographically (dictionary order).
Using sorted() Function
The sorted() function returns a new sorted list containing all the items from the original list.
Python a = ["banana", "apple", "cherry"] # Sorting the list res = sorted(a) print(res)
Output['apple', 'banana', 'cherry']
Explanation:
- sorted(a) creates a new sorted list without modifying the original a.
- Strings are sorted lexicographically (dictionary order).
Note: The main difference between sort() and sorted() is that sort() modifies the list in place without returning a new list, while sorted() creates a new sorted list from any iterable without modifying the original.
Sorting in Reverse Order
To sort strings in descending order, we can use the reverse=True parameter.
Python a = ["banana", "apple", "cherry"] # Sorting in reverse order res = sorted(a, reverse=True) print(res)
Output['cherry', 'banana', 'apple']
Case-Insensitive Sorting
String sorting in Python is case-sensitive by default. To perform a case-insensitive sort, we can use the key=str.lower parameter in sorted() function.
Python a = ["Banana", "apple", "Cherry"] res = sorted(a, key=str.lower) print(res)
Output['apple', 'Banana', 'Cherry']
Explanation: key=str.lower ensures that sorting is performed based on lowercase equivalents of strings.
Sorting by String Length
We can sort a list of strings by their lengths using the key=len parameter in sorted() function.
Python a = ["banana", "apple", "kiwi"] # Sorting by length res = sorted(a, key=len) print(res)
Output['kiwi', 'apple', 'banana']
Explanation: key=len sorts the strings based on their length in ascending order.
Sorting using Custom Key
We can also use any custom sorting logic using a key function.
Python a = ["banana", "apple", "cherry"] # Sorting by last character res = sorted(a, key=lambda s: s[-1]) print(res)
Output['banana', 'apple', 'cherry']
Explanation: lambda s: s[-1] extracts the last character of each string for comparison.
Similar Reads
Python | Sort each String in String list Sometimes, while working with Python, we can have a problem in which we need to perform the sort operation in all the Strings that are present in a list. This problem can occur in general programming and web development. Let's discuss certain ways in which this problem can be solved. Method #1 : Usi
4 min read
Python | Sort list of dates given as strings To sort a list of dates given as strings in Python, we can convert the date strings to datetime objects for accurate comparison. Once converted, the list can be sorted using Python's built-in sorted() or list.sort() functions. This ensures the dates are sorted chronologically.Using pandas.to_datetim
2 min read
Python | Sort all sublists in given list of strings Sorting sublists in a list of strings refers to arranging the elements within each sublist in a specific order. There are multiple ways to sort each list in alphabetical order, let's understand each one by one.Using list comprehensionList comprehension with sorted() allows efficient sorting of each
2 min read
Sort Numeric Strings in a List - Python We are given a list of numeric strings and our task is to sort the list based on their numeric values rather than their lexicographical order. For example, if we have: a = ["10", "2", "30", "4"] then the expected output should be: ["2", "4", "10", "30"] because numerically, 2 < 4 < 10 < 30.
2 min read
List of strings in Python A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples.Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings.Pythona =
2 min read
How To Sort List Of Strings In Alphabetically When working with Python programs, be it any machine learning program or simple text processing program, you might have come across a need to sort a list of strings alphabetically, either in ascending or reverse order. Strings are sorted alphabetically based on their initial letter (a-z or A-Z). But
3 min read