How to Sort a List Alphabetically in Python [4 Ways]

Here are four ways to sort a list alphabetically in Python.

  1. Using the sorted() method
  2. Using the sort() method
  3. Sort a list alphabetically using a key
  4. Using a custom function

Method 1: Using the sorted() method

To sort a list alphabetically in Python, the easiest way is to use the “sorted()” method. 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() function will sort it alphabetically. You can add reverse=False/True in the brackets to reverse the order of items: list.sort(reverse=False).

Method 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 the 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 easy to sort a list alphabetically in ascending or descending order in Python.

Method 3: Sort a List Alphabetically in Python with key

main_list = [('Krunal', 'Rajkot', 'Data Analyst'),
             ('Ankit', 'Ahmedabad', 'Software Developer'),
             ('Rushabh', 'Singapore', 'Data Scientist')]

sort_mylist = sorted(main_list, key=lambda x: x[1])

print(sort_mylist)

Output

[('Ankit', 'Ahmedabad', 'Software Developer'), 
 ('Krunal', 'Rajkot', 'Data Analyst'), 
 ('Rushabh', 'Singapore', 'Data Scientist')]

Method 4: Using a custom function

In this method, we will sort a list alphabetically in Python without the “sort()” function.

def sort(lst):
 if not lst:
 return []
 return (sort([x for x in lst[1:] if x < lst[0]])
 + [lst[0]] +
 sort([x for x in lst[1:] if x >= lst[0]]))


s = ["Oppenheimer", "Louis", "Leslie", "Kitty", "Albert"]
print(sort(s))

Output

['Albert', 'Kitty', 'Leslie', 'Louis', 'Oppenheimer']

That’s it!

Related posts

How to Sort a String

Leave a Comment

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