Sort List of Strings in Python | Explained With Examples

Whenever data is being processed, sorting or ordering is a very important step. Data sorting makes data more readable and easy to analyze and process. Python supports an inbuilt function “sort()” used to sort list data in ascending and descending order.

In this write-up, we will demonstrate how to sort a Python list of strings with multiple examples. The topics discussed in this guide are below:

Let’s begin our Tutorial!

How to Sort a List of Strings in Python?

To sort a list of strings, inbuilt functions “sort()” and “sorted()” are used in the Python Program. The list of strings can be sorted according to the requirements, such as in alphabetical order, reversed alphabetical order, length, etc. The syntax of the “sort()” and “sorted()” functions are shown below:

list.sort(key=..., reverse=...) Or sorted(list, key=..., reverse=...)

In the above syntax:

  • The parameter “key” is used to define specific sorting criteria.
  • The parameter “reverse” has a default value of “False”, and the “reverse=True” will sort the list in descending order.
  • The parameter “list” in the “sorted()” function takes the input variable of iterable sequences such as list, tuple, dictionary, etc.

Let’s get started with our first example:

Example 1: Sort a List of Strings Using sort() Function: Default Parameter

In the example given below, the inbuilt “sort()” function sorts the list of strings by taking the value of the list variable as an argument. The default value of the “sort()” function parameter returns the list of strings in alphabetical order.

Code:

List_value = ['Python', 'Guide', 'Provided', 'By', 'ILF']

# Using sort() function
List_value.sort()
print(List_value)

In the above code:

  • The list containing string value is initialized in the program.
  • The “list.sort()” function is used to sort the strings in alphabetical order.

Note: The “sort()” function prefers the upper case alphabet when there is a list of strings containing both upper and lower case elements.

Output:

In the above output, the list of strings has been sorted alphabetically using the sort() function.

Example 2: Sort a List of Strings in Reverse Order Using sort()

In the example given below, the “sort()” function sorts the string value of the list in reverse alphabetical order. Specify the reverse parameter value as “True” in this scenario. Let’s see this example via the following code:

Code:

List_value = ['Python', 'Guide', 'Provided', 'By', 'ILF']

# Using sort() function
List_value.sort(reverse = True)
print(List_value)

In the above code:

  • The list of strings is initialized in the program.
  • The “sort()” function with the parameter value of “reverse = True” returns the list of strings in reverse alphabetical order.

Output:

In the above output, the list of strings has been sorted in reverse alphabetical order using the sort() function.

Example 3: Sort a List of Strings by Using sort() Function: Key Parameter

In the example code shown below, the “sort()” function is defined with its parameter “key”. The list of strings is sorted according to the specific key value, defined as “len or length”.

Code:

List_value = ['Python', 'Guide', 'Provided', 'By', 'ILF']

# Using key value as len
List_value.sort(key = len)
print(List_value)

In the above code:

  • The list of strings is initialized in the program
  • The “key” parameter value of the “sort()” function is used to sort the list of strings according to a specific key. The “key” parameter value is initialized with the “len” value, which will sort the list of strings according to the length of the strings.
  • The string with a smaller length will come/sort first in this case.

Output:

In the above output, the list of strings have been sorted according to the length of the string using the sort() function.

Example 4: Sort a List of Numeric Strings by Using sort() Key Parameter

In the example given below, the float value of the given string will be sorted in ascending order. The list of strings in the following code sorts according to the “key” parameter value.

Code:

List_value = ['53', '1.3', '31', '37', '5.5']

# Using key value as float
List_value.sort(key = float)
print(List_value)

In the above code:

  • The list value containing numeric strings is initialized in the program.
  • The “sort()” function with parameter “key” value is defined as “float”. The list of numeric strings will be sorted according to ascending order of input numbers.
  • Because the key value is “float”, the preference will be given to the “float” value instead of integers. As a result, all the float values will be sorted first, while the integer values will come/sort after the float values.

Output:

In the above output, the list of strings has been sorted according to the float values using the sort() function.

Example 5: Sort a List of Strings by Using the sorted() Function

In the example given below, the list of strings is sorted by using the “for loop” with a combination of the “sorted()” function. The difference between the “sort()” and the “sorted()” function is that the “sorted()” function retrieves the sorted list and does not modify the original list. The other function, named the “sort(),” changes the original list and does not return any value. The example shown below provides the concept of the “sorted()” function:

Code:

List_value = ['Python', 'Guide', 'Provided', 'By', 'ILF']

# Using sorted() function
for i in sorted(List_value):
    print(i, end=" ")

In the above code:

  • The list containing string values is initialized in the program.
  • The “for loop” with the combination of the “sorted()” function is utilized. The “for loop” iterates over the sorted list and returns all the values of the string element on the screen.

Output:

From the output, it is verified that the list of strings is sorted in alphabetical order.

That is from this Python guide!

Conclusion

To sort the list of strings in Python, an inbuilt “list.sort()” and “sorted()” functions are utilized. The “sort()” function sorts the list of strings according to specific “key” parameter values and “reverse” parameter values. To sort according to length, integer, or float value, the “sort()” function utilized the “key” parameter. Similarly, to sort a list in descending order, the “reverse=False” value is utilized in the program. This article presented a detailed overview of how to sort a list of strings with multiple examples.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×