How to Sort a List Alphabetically in Python

Manav Narula Feb 02, 2024
  1. Use the sort() Method to Sort a List Alphabetically in Python
  2. Use the sorted() Function to Sort a List Alphabetically in Python
  3. Use Quick Sort Algorithm to Sort a List Alphabetically in Python
How to Sort a List Alphabetically in Python

In this tutorial, we will discuss how to sort a list containing strings alphabetically using the sort() and sorted() functions and using the quick sort algorithm.

Both sort() and sorted() can perform the same function, with the main difference between them being that the sort() function sorts the original list, whereas the sorted() function creates a new list.

Use the sort() Method to Sort a List Alphabetically in Python

The sort() method of the list object is used to sort a list. By default, it sorts the list in ascending order. For example:

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
my_list.sort()
print(my_list)

Output:

['Baron', 'Jack', 'Jay', 'Mark', 'Sam']

To sort the list in the reverse order we can use the reverse parameter and set it to True. It is False by default. For example:

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
my_list.sort(reverse=True)
print(my_list)

Output:

['Sam', 'Mark', 'Jay', 'Jack', 'Baron']

Notice that the order of the sorted list has reversed. We can also specify the condition to sort using the key parameter. In the code below, we will sort the list based on the length of the string of each element in ascending order:

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
my_list.sort(key=len)
print(my_list)

Output:

['Sam', 'Jay', 'Mark', 'Jack', 'Baron']

Use the sorted() Function to Sort a List Alphabetically in Python

The sorted() function also sorts a list in the required order, but it creates a new list and doesn’t alter the original list. To sort alphabetically in ascending order, we simply pass it to the function as shown below.

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
sorted_list = sorted(my_list)
print(sorted_list)

Output:

['Baron', 'Jack', 'Jay', 'Mark', 'Sam']

Similarly to the sort() method, we can sort in descending order using the reverse parameter. For example:

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
sorted_list = sorted(my_list, reverse=True)
print(sorted_list)

Output:

['Sam', 'Mark', 'Jay', 'Jack', 'Baron']

We can also use the key parameter to specify the condition for sorting as we did with the sort() function. For example:

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]
sorted_list = sorted(my_list, reverse=True, key=len)
print(sorted_list)

Output:

['Baron', 'Mark', 'Jack', 'Sam', 'Jay']

Use Quick Sort Algorithm to Sort a List Alphabetically in Python

We can also use the quick sort algorithm to sort a list. This method may be unconventional, but it is worth noting that other sorting techniques like merge sort, selection sort, insertion sort, heap sort, and bubble sort can also achieve this. The following code shows a function that implements the quicksort method to sort a list in Python.

my_list = ["Jack", "Sam", "Jay", "Mark", "Baron"]


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


print(quicksort(my_list))

Output:

['Baron', 'Jack', 'Jay', 'Mark', 'Sam']
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python List