To sort a list of strings in Python you can use the sort()
method. This method will order the list of strings in place, meaning that it will modify the original list and you won’t need to create a new list. You can also use the sorted()
function to sort a list of strings, this returns a new list after sorting.
There are a few ways to sort the list of strings in Python.
- Sorting in alphabetical/reverse order: You can use the built-in sort() or sorted() functions to sort a list of strings in alphabetical or reverse alphabetical order.
- Based on the length of the string character: You can use the key argument of the sort() or sorted() function to sort a list of strings based on the length of the strings.
- Sorting the integer values in a list of strings: If all of the strings in the list can be cast to integers, you can sort the list of strings by the integer values.
1. Quick Examples of Sort List of Strings
If you are in a hurry, below are some quick examples of the Python sort list of strings.
# Below are the quick examples
# Example 1: Sort list of strings
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology.sort()
# Example 2: Sort list by length of strings
technology.sort(key = len)
# Example 3: Sort string by integer value use key as int
strings = ['12','34','5','26','76','18','63']
strings.sort(key = int)
# Example 4: Sort string in reverse order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology.sort(reverse = True)
# Example 5: Using sorted() method
sorted_list = sorted(technology)
# Example 6: Sorted string by integer value use key = int
strings = sorted(strings, key=int)
# Example 7: Sorted list of strings in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology = sorted(technology, reverse=True)
2. Python Sort List of Strings
The sort() function is used to sort the list of strings in ascending order in python. This function modifies the original list in place and does not return a new list. For example, using the sort()
function without any parameters to sort a list of strings in ascending order.
# Use sort() method
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology.sort()
print(technology)
# Output
# ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark']
3. Sort List of Strings by sorted() Method
Alternatively, you can use the python sorted() built-in function to order the technology list of stings. It returns a new list containing all the elements from the technology list in sorted order.
# Using sorted() method
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
sorted_list = sorted(technology)
print(sorted_list)
# Output:
# ['Hadoop', 'Hyperion', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark']
4. Sort List by Length of Strings
To sort a list of strings based on the length of the strings, you can use the len()
function as the key
for the sort()
function. For example, the list is sorted based on the length of the strings, with the shortest string coming first and the longest string coming last.
# Sort list by length of strings
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology.sort(key = len)
print(technology)
# Output
# ['Java', 'Spark', 'NumPy', 'Hadoop', 'Pandas', 'Pyspark', 'Hyperion']
5. Sort Strings using a Function
Similarly, If you want to sort a list of strings based on the integer value of the strings, you can convert the strings to integers by using int()
function as the key
for the sort()
method or sorted()
function. This sorts the list by numbers.
# Sort string by integer value use key as int
strings = ['12','34','5','26','76','18','63']
strings.sort(key = int)
print(strings)
# Sorted string by integer value use key = int
strings = sorted(strings, key=int)
print(strings)
# Output:
# ['5', '12', '18', '26', '34', '63', '76']
6. Sort Strings in Descending Order
To sort a list of strings in descending or reverse order, you can pass the reverse=True
argument to the sort()
method or sorted()
function. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value (for string Z to A).
# Sort in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology.sort(reverse = True)
print(technology)
# Sorted list of strings in descending order
technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
technology = sorted(technology, reverse=True)
print(technology)
# Output
# ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop']
Conclusion
In this article, I have explained how to sort the list of strings in python, first, I have covered using list.sort() function and python built-in function sorted().
Happy Learning !!
Related Articles
- How to Sort a List of Tuples in Python
- Python Sort List Descending
- Python Sort Dictionary by Key
- Python Sort Array Values
- Python Sort List in Reverse Order
- Python Sort List of Numbers or Integers
- Python Sort List Alphabetically
- Sort using Lambda in Python
- How to Sort Dictionary by Value in Python
- Sort Set of Values in Python