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

In this Python tutorial, I explain how to sort a list alphabetically in Python using different methods with some illustrative examples.

To sort a list alphabetically in Python we can use two direct methods like sort() and sorted() which returns the sorted list, or we can use the max() and min() functions within a while loop, and we can even create a custom function that will not use any built-in function of Python for sorting.

Sort a List Alphabetically in Python Methods

There are five different methods present to sort a list alphabetically in Python.

  1. sort() function
  2. sorted() function
  3. max() function
  4. min() function
  5. Custom function

Let’s see them one by one in detail with some illustrative examples:

Method 1: Python sort list alphabetically using sort() function

In Python, the sort() method is used to arrange the elements of a list in a specified order, either ascending or descending. When applied to a Python list containing string elements, sort() can order the items alphabetically.

Example: Consider, we have a list of data in the form of a Python list, and we have to sort it alphabetically for some analysis.

states = ["Texas", "Alabama", "Ohio", "Florida"]
states.sort()
print('Sorted states names:', states)

Output: The implementation of the Python is:

Sorted states names: ['Alabama', 'Florida', 'Ohio', 'Texas']
How to sort a list alphabetically in Python

Note: To sort in reverse order (descending), we can use the reverse=True argument in the Python sort() function.

Sort a List Alphabetically in Python

This way we can use the sort() function to sort a list alphabetically in Python.

Method 2: Python data structure sort list alphabetically using sorted() function

In Python, the sorted() function returns a new sorted list from the items in any iterable (like list, tuple, string, etc.). When sorting a list alphabetically, the sorted() function compares the elements using their ASCII values to determine the order.

Example: Consider a situation where we have a Python list and must sort it alphabetically using the sorted() function.

states = ["Wyoming", "Colorado", "Nevada", "Montana"]
sorted_states = sorted(states)
print('Sorted states list: ', sorted_states)

Output: The implementation of the Python code with a screenshot is as follows:

Sorted states list:  ['Colorado', 'Montana', 'Nevada', 'Wyoming']
Sorting a List Alphabetically in Python from a Certain Character

Note: However, sorted() differentiates between uppercase and lowercase letters since their Unicode code points are different (uppercase letters come before lowercase ones). To sort a list alphabetically without considering the case, we can use the str.lower method as a key argument.

states = ["Wyoming", "colorado", "nevada", "Montana"]
sorted_states = sorted(states, key=str.lower)
print('Sorted states list: ', sorted_states)
How to sort list of strings alphabetically with upper and lower values

To sort a list alphabetically in Python, this way we can use the sorted() function.

Method 3: Sort list of strings alphabetically using max() function

To sort a list alphabetically in Python using the max() function, we can implement a function that repeatedly finds the maximum element in the list (considering the alphabetical order).

And builds a new list with the sorted elements by inserting them one by one in an empty list using the insert() function in Python. In the meantime, we will also remove that item from the list, so that we don’t get the same result all the time using the remove() function.

Example: Here, we have a list in Python, and have to sort it alphabetically using the max() function in Python.

def sort_list_alphabetically(input_list):
    sorted_list = []
    while input_list:
        max_element = max(input_list)
        sorted_list.insert(0, max_element)
        input_list.remove(max_element)
    return sorted_list

us_companies= ['Apple', 'Google', 'Amazon', 'Microsoft', 'Facebook']

sorted_list = sort_list_alphabetically(us_companies)
print('Sorted names of the companies: ', sorted_list)

Output: The output of the above-written Python code is:

Sorted names of the companies:  ['Amazon', 'Apple', 'Facebook', 'Google', 'Microsoft']
Python Program to Sort Words in Alphabetic Order

This way we can simply use the max() function within a while loop and with insert() and remove() functions to sort a list alphabetically in Python.

Method 4: Sort List of Strings Alphabetically in Python using min() function

To sort a list alphabetically using the min() function in Python, we can implement a function that repeatedly finds the minimum element in the list (according to alphabetical order) and appends it to a new sorted list using the append() function in Python.

In the meantime, we will also remove that item from the list, so that we don’t get the same result all the time using the remove() function.

Example: For instance, we have a list and we have to sort it alphabetically using the min() function through Python.

def sort_list_alphabetically_using_min(input_list):
    temp_list = input_list[:]
    sorted_list = []
    while temp_list:
        min_element = min(temp_list)
        sorted_list.append(min_element)
        temp_list.remove(min_element)
    return sorted_list

us_landmarks = ['Statue of Liberty', 'Mount Rushmore', 'Golden Gate Bridge', 'White House']
sorted_list = sort_list_alphabetically_using_min(us_landmarks)
print('Sorted names of different landmarks:\n', sorted_list)

Output: The Output of the above-written code in Pycharm editor with a screenshot is:

Sorted names of different landmarks:
 ['Golden Gate Bridge', 'Mount Rushmore', 'Statue of Liberty', 'White House']
how to sort list in alphabetical order in python

This way we can use the min() function in Python within a while loop with append() and remove() functions to sort a list alphabetically in Python.

Method 5: Sort list of strings alphabetically in Python using a custom function

Creating a custom sort function means writing step-by-step instructions for how to arrange the list alphabetically without using any built-in methods Present in Python.

This function uses a simple bubble sort algorithm, which repeatedly steps through the list in Python, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the Python list is repeated until the list is sorted.

Example: Consider a case, in which we have a arrange a list alphabetically without using any built-in functions present in Python.

def custom_sort(list_to_sort):
    n = len(list_to_sort)
    for i in range(n):
        for j in range(0, n-i-1):
            if list_to_sort[j] > list_to_sort[j+1]:
                list_to_sort[j], list_to_sort[j+1] = list_to_sort[j+1], list_to_sort[j]

us_rivers = ['Mississippi River', 'Missouri River', 'Yukon River', 'Rio Grande', 'Colorado River']
custom_sort(us_rivers)
print('Sorted names of the river alphabetically:\n', us_rivers)

Output: The implementation of the code in Python Pycharm editor with the screenshot is mentioned below:

Sorted names of the river alphabetically:
 ['Colorado River', 'Mississippi River', 'Missouri River', 'Rio Grande', 'Yukon River']
How to sort list alphabetically Python

This way we can create a custom function for bubble sorting without using any built-in functions present to sort a list alphabetically in Python.

Conclusion

This Python programming tutorial has explained how to sort a list alphabetically in Python using five different methods such as built-in functions like sort() and sorted(), which are simple to use and handle most of the sorting needs. The max() and min() functions can further aid in finding the alphabetical first or last elements. For customized sorting logic or learning purposes, creating a custom sorting function can be a valuable exercise

It’s important to choose the right approach based on the needs of the task, whether it’s modifying the original list, creating a sorted copy, or implementing a specific sorting algorithm.

You may also like to read these articles: