Python List sort() Method

The Python list sort () method will help to sort the elements of the list. Hence, it will sort the ascending order by default but will sort the values in the descending values. This article has listed the Python List Sort Method.

What is the Python List sort () Method?

Python list sort method will sort the elements of the list. The list sort() is referred to as the in-build function in Python and will sort the values of the list in ascending or descending order. Whereas, by default, it will then sort the values in ascending order. So, it is referred to as a very useful and simple list operation in Python.

Let us look into the example provided below

Example

# Example list
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

# Using the sort() method
my_list.sort()

# Displaying the sorted list
print("Sorted List:", my_list)

Output

Sorted List: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

What is the Syntax of the Python List Sort()?

List_name.sort(reverse=True/False, key=myFunc)

How to Use the List Sort () Function

The list sort function in Python will be very easy. Hence, it is required to call the function with the list object. Parameters can be used.

Python list sort() Examples and Use

The use case scenarios of the list sort() method with the examples. Some of the examples are given below:

  1. Sort a List of Numbers in Ascending Order
  2. Sort a List of Alphabets In Ascending Order
  3. Sort a List in Python in Descending Order 
  4. Sort a List in Python By Key

What are Python List sort numbers in Ascending Order?

The sort() method by default will sort elements in ascending order. Check the example given below:

Example

# Sample list of numbers
numbers = [5, 2, 8, 1, 3]

# Sorting the list in ascending order
numbers.sort()

# Displaying the sorted list
print("Sorted Numbers in Ascending Order:", numbers)

Output

Sorted Numbers in Ascending Order: [1, 2, 3, 5, 8]

What is Sort a List of Alphabets in the Ascending Order?

The sort method will sort the list in the order from the A-Z in the alphabet.

Example

# Sample list of alphabets
alphabets = ['c', 'a', 'b', 'f', 'e', 'd']

# Sorting the list in ascending order
alphabets.sort()

# Displaying the sorted list
print("Sorted Alphabets in Ascending Order:", alphabets)

Output

Sorted Alphabets in Ascending Order: ['a', 'b', 'c', 'd', 'e', 'f']

What is Python Sort List in the Descending Order?

In Python, it is possible to sort the list of numbers in descending order and the same will be for alphabets. Thus, to do this, we need to pass the reverse=True and it will sort the numbers or the alphabet in descending order.

Example

# Sample list of numbers
numbers = [5, 2, 8, 1, 3]

# Sorting the list in descending order
numbers.sort(reverse=True)

# Displaying the sorted list
print("Sorted Numbers in Descending Order:", numbers)

Output

Sorted Numbers in Descending Order: [8, 5, 3, 2, 1]

Python sort List by Key 

We will sort the elements with the help of a function and are based on passing the function to the key parameter of the sort() function.

Example

# Sample list of dictionaries
people = [
    {'name': 'John', 'age': 30},
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 35},
]

# Sorting the list of dictionaries by the 'age' key
sorted_people = sorted(people, key=lambda x: x['age'])

# Displaying the sorted list
print("Sorted People by Age:", sorted_people)

Output

Sorted People by Age: [{'name': 'Alice', 'age': 25}, {'name': 'John', 'age': 30}, {'name': 'Bob', 'age': 35}]

Conclusion

In conclusion, the sort() method in Python is a straightforward and convenient way to sort a list in place. It arranges the elements of the list in ascending order by default but can be customized for descending order by providing the reverse=True parameter.

Hence, this method will modify the original list and won’t create a new one. It is a useful tool for quickly organizing numerical or alphabetical data within a list without the need for additional sorting functions.

Python List sort() Method-FAQs

Q1.What does sort () do in Python?

Ans. The sort() method will sort the list in the ascending order.

Q2.How will the Python method sort () sort a list of strings?

Ans. The sorted() function will return the sorted list of the specified iterable object.

Q3.What is the use of the sort () function?

Ans. Sort will return the sorted array of the elements in the array.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment