JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Sorting a list alphabetically in Python

Author:JIYIK Last Updated:2025/05/09 Views:

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

sort()Both sorted()perform the same function, the main difference between them is that sort()the function sorts the original list, while sorted()the function creates a new list.


sort()Sort a list alphabetically using the method in Python

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

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

Output:

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

To sort the list in reverse order, we can use reversethe -p parameter and set it to True. By default, it is False. 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 been reversed. We can also keyspecify the criteria for sorting using the parameter. In the following code, we will sort the list in ascending order based on the string length of each element.

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

Output:

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

sorted()Sort a list alphabetically in Python using the function

sorted()The function also sorts the list in the required order, but it creates a new list and does not change the original list. If we want to sort in ascending alphabetical order, we just 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']

Similar to sort()the method, we can use reversethe parameter to sort in descending order. For example, we can use reversethe parameter to sort in descending order.

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 sorting condition, just like using 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']

Sort a list alphabetically using the Quick Sort algorithm in Python

We can also sort the list using the quick sort algorithm. This approach may be unconventional, but it is worth noting that other sorting techniques such as merge sort, selection sort, insertion sort, heap sort, and bubble sort can also achieve this purpose. The following code shows a function that implements the quick 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']

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Finding a string in a list in Python

Publish Date:2025/05/09 Views:75 Category:Python

This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem

Getting list shape in Python

Publish Date:2025/05/09 Views:139 Category:Python

In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim

Adding multiple elements to a list in Python

Publish Date:2025/05/09 Views:180 Category:Python

List is a mutable data structure in Python. It can contain values ​​of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi

Get the index of the maximum and minimum values in a list in Python

Publish Date:2025/05/09 Views:117 Category:Python

In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi

List of numbers from 1 to N in Python

Publish Date:2025/05/09 Views:116 Category:Python

This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until

Convert List to Pandas DataFrame in Python

Publish Date:2025/05/09 Views:133 Category:Python

This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i

Sorting a list by another list in Python

Publish Date:2025/05/09 Views:148 Category:Python

Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values ​​in another list in this art

Normalizing a list of numbers in Python

Publish Date:2025/05/09 Views:134 Category:Python

Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn

How to create a list of a specific size in Python

Publish Date:2025/05/09 Views:195 Category:Python

Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial