2 Easy Ways to Sort List Alphabetically in Python

Introduction

Sorting a list alphabetically allows you to organize a list of elements in a logical and easy-to-read manner. For example, if you have a list of names, sorting the list alphabetically will allow you to quickly find a specific name by searching through the list systematically.

A sorting algorithm arranges the elements of a list in a specific order.

Sorting algorithms assist in reducing problem complexity and can even optimize other methods.

Government agencies, business corporations, and financial firms arrange their data by sorting it. Sorted data makes it easier for algorithms to find later.

Sorting is one of the most important topics in computer science due to its complexity. Sorting can be defined as the arrangement of elements in an ordered sequence.

In this example, we will see ways to sort a list alphabetically in ascending and descending order.

How to sort a list alphabetically in Python

There are two easy methods to sort a list alphabetically in Python.

  1. Using the sorted() method
  2. Using the list.sort() method

Technique 1: Using the sorted() method

To sort a list alphabetically in Python, use the sorted() function. The sorted() function sorts the given iterable object in a specific order, either ascending or descending.

The sorted(iterable, key=None) method takes an optional key that specifies how to sort.

To disregard capitalization when sorting a list, set the key to str.

Follow the below steps to sort a list alphabetically.

Step 1: Create a list of strings

Use the square brackets [ ] and add the strings separated by a comma to create a list of strings.

data = ['Elle', 'Miles', 'Kratos', 'Joel', 'Peter', 'Nathan']

Step 2: Pass the list of strings to the sorted() function

data = ['Elle', 'Miles', 'Kratos', 'Joel', 'Peter', 'Nathan']

print(sorted(data))

Output

['Elle', 'Joel', 'Kratos', 'Miles', 'Nathan', 'Peter']

And it returns a list of strings in ascending order.

One thing to note here is that we have not mixed a list with upper and lowercase strings.

data = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']

print(sorted(data))

Output

['Elle', 'Joel', 'Nathan', 'kratos', 'miles', 'peter']

You can see that now the sorting is messed up. So let’s resolve this issue.

Step 3: Add a key parameter

As you can see, words that start with an uppercase letter get preference over those beginning with a lowercase letter.

 To sort them independently, add the key parameter.

data = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']

print(sorted(data, key=str.lower))

Output

['Elle', 'Joel', 'kratos', 'miles', 'Nathan', 'peter']

Step 4: Pass the reverse = True to the sorted() function

To reverse the order of the list, add the reverse argument and set it to True.

data = ['Elle', 'miles', 'kratos', 'Joel', 'peter', 'Nathan']

print(sorted(data, reverse=True))

Output

['peter', 'miles', 'kratos', 'Nathan', 'Joel', 'Elle']

The list.sort() will sort it alphabetically. You can add reverse=False/True in the brackets to reverse the order of items: list.sort(reverse=False).

Technique 2: Using the list sort() method

The list.sort() is a built-in Python function that sorts the elements of a list in low to high order.

If the list is of numbers, then by default, they will be sorted in increasing order.

Check out the below step-by-step guide to sort lists alphabetically using the list.sort() method.

Step 1: Create a list of strings with mixed cases

As we saw earlier, use the square brackets [] to create a list of strings with mixed cases.

data = ['Elle', 'miles', 'Kratos', 'joel', 'Peter', 'nathan']

You can see that list of strings consists of upper and lower cases.

Step 2: Use the .sort() method

You can call the .sort() method on the list and pass in the optional parameter “key=str.lower” that will tell the .sort() method to sort the list alphabetically, ignoring the case of the words.

data = ['Elle', 'miles', 'Kratos', 'joel', 'Peter', 'nathan']

data.sort(key=str.lower)
print(data)

Output

['Elle', 'joel', 'Kratos', 'miles', 'nathan', 'Peter']

You can see that it is sorted alphabetically regardless of the cases of the string.

Step 3: Sort the list in descending order

To sort the list in descending order, pass the optional parameter “reverse=True” to the .sort() method.

data = ['Elle', 'miles', 'Kratos', 'joel', 'Peter', 'nathan']

data.sort(key=str.lower, reverse=True)
print(data)

Output

['Peter', 'nathan', 'miles', 'Kratos', 'joel', 'Elle']

You can see the list.sort() function makes it very easy to sort a list alphabetically in ascending or descending order in Python.

Solidity of Sorting Algorithms in Python

The solidity maintains the original order of elements with equal values during the sorting process.

Stable sorting implies that two objects with identical keys appear in the sorted output in the same order as they did in the original input.

A stable sort, in other words, assures that the original order of data with the same rank is preserved in the output.

Stable sorting algorithms are required to keep equal elements in order.

They must not modify the relative order of the compared elements to be considered as such. This is useful for sorting, for example, if we need to do it in many passes (for example, sorting by city and occupation) or something like that.

A non-stable sort, on the other hand, may shuffle one element while sorting by another.

In this situation, sorting by the city may shuffle the name order, resulting in a list where the persons are sorted by the city but not by name within a city, even though they were initially sorted by name.

Conclusion

The best and most efficient way to sort a list alphabetically in Python is to use the sorted() function.

The sort() and sorted() functions prioritize uppercase strings.

However, we might want case-insensitive sorting. If that is the case, then setting the key argument to the str.lower() function will ignore the string cases and sort the list elements alphabetically.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.