Here are ways to sort a list alphabetically in Python:
- Using sorted()
- Using sort()
- Using a lambda function
Method 1: Using sorted()
The sorted() function returns a new list in ascending order.
Visual Representation
Example
list_of_strings = ['Elle', 'Miles', 'Kratos', 'Joel', 'Peter', 'Nathan']
print("Original List: ", list_of_strings)
sorted_list = sorted(list_of_strings)
print("Sorted List:", sorted_list)
Output
Original List: ['Elle', 'Miles', 'Kratos', 'Joel', 'Peter', 'Nathan']
Sorted List: ['Elle', 'Joel', 'Kratos', 'Miles', 'Nathan', 'Peter']
One thing to note here is that we have not mixed a list with upper and lowercase strings.
Visual Representation
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Original List: ", list_of_strings)
sorted_list = sorted(list_of_strings)
print("Sorted List:", sorted_list)
Output
Original List: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
Sorted List: ['Elle', 'Joel', 'Nathan', 'kratos', 'miles', 'peter']
As you can see, uppercase letters are prioritized due to their lower ASCII values compared to lowercase letters.
Consequently, strings starting with uppercase letters are sorted before those with lowercase initial letters.
To sort them in a case-insensitive manner, pass the “key=str.lower” parameter.
Visual Representation
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Original List: ", list_of_strings)
sorted_list = sorted(list_of_strings, key=str.lower)
print("Sorted List:", sorted_list)
Output
Original List: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
Sorted List: ['Elle', 'Joel', 'kratos', 'miles', 'Nathan', 'peter']
To reverse the order of the list, add the reverse argument and set it to True.
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Original List: ", list_of_strings)
sorted_list = sorted(list_of_strings, reverse=True)
print("Reverse Sorted List:", sorted_list)
Output
Original List: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
Reverse Sorted List: ['peter', 'miles', 'kratos', 'Nathan', 'Joel', 'Elle']
Method 2: Using sort()
The sort() function, by default, sorts the original list in ascending order. Therefore, if you want to preserve the original list, avoid using this method.
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Before sort: ", list_of_strings)
list_of_strings.sort()
print("After sort:", list_of_strings)
Output
Before sort: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
After sort: ['Elle', 'Joel', 'Nathan', 'kratos', 'miles', 'peter']
Visual Representation
You can pass the optional parameter “key=str.lower” to sort the list alphabetically, ignoring the case of the words.
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Before sort: ", list_of_strings)
list_of_strings.sort(key=str.lower)
print("After sort:", list_of_strings)
Output
Before sort: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
After sort: ['Elle', 'Joel', 'kratos', 'miles', 'Nathan', 'peter']
To sort the list in descending order, pass the optional parameter “reverse=True”.
Visual Representation
Example
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Before sort: ", list_of_strings)
list_of_strings.sort(key=str.lower, reverse=True)
print("After sort:", list_of_strings)
Output
Before sort: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
After sort: ['peter', 'Nathan', 'miles', 'kratos', 'Joel', 'Elle']
Method 3: Using a Lambda Function
The lambda function converts each element of the list to lowercase, ensuring that the list is sorted alphabetically without considering the case of the letters.
Example 1: Using with sorted()
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Original List: ", list_of_strings)
sorted_list = sorted(list_of_strings, key=lambda x: x.lower())
print("Sorted List:", sorted_list)
Output
Original List: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
Sorted List: ['Elle', 'Joel', 'kratos', 'miles', 'Nathan', 'peter']
Example 2: Using with sort()
list_of_strings = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
print("Before Sort: ", list_of_strings)
list_of_strings.sort(key=lambda x: x.lower())
print("After Sort:", list_of_strings)
Output
Before Sort: ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']
After Sort: ['Elle', 'Joel', 'kratos', 'miles', 'Nathan', 'peter']