Python - How To Sort a List Alphabetically in Python

ID : 319

viewed : 80

Tags : PythonPython List

vote vote


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) 


['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) 


['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) 


['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) 


['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) 


['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) 


['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)) 


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

  • Related HOW TO?