Sorting a list in Python is an operation to arrange the list either in ascending or descending order based on the elements. There are various methods available in Python through which we can sort the list in Python. In this short article, we will learn various methods of sorting a list in Python by solving various examples.
Introduction to sorting a list in Python
Sorting is a common operation in computer programming, and it refers to arranging data in a particular format. In Python, we can sort a list of items using the built-in sorted function. There are various ways to sort a list in Python, and in this article, we will explore them all in detail. We will also see how to sort a list of lists, a list of tuples, and a list of dictionaries in Python.

Sorting a List in Python using the Sorted Function
The sorted() function is a built-in function in Python that takes an iterable (such as a list, tuple, or string) and returns a new sorted list with the elements in ascending order.
Here’s the basic syntax for using sorted():
#Sorting a List in Python sorted(iterable, key=None, reverse=False)
iterable is the list, tuple, or other iterable that you want to sort.
key is an optional parameter that specifies a function that returns a value that should be used for sorting. This can be useful for sorting a list of objects based on an attribute of the object.
reverse is an optional boolean parameter that specifies whether the list should be sorted in ascending (False) or descending (True) order. The default value is False.
Here are some examples of how to use sorted():
# Sort a list of numbers in ascending order numbers = [3, 1, 4, 2] #Sorting a List in Python sorted_numbers = sorted(numbers) #printing print(sorted_numbers) # [1, 2, 3, 4]
Here is another example of sorted() method used in Python.
# Sort a list of strings in ascending order words = ['cat', 'apple', 'zoo', 'book'] #Sorting a List in Python sorted_words = sorted(words) #printing print(sorted_words) # ['apple', 'book', 'cat', 'zoo']
You can check another example of a sorted method in Python. This time we will apply the sorted() method on the class.
class Person: def __init__(self, name, age): self.name = name self.age = age # Sort a list of Person objects by their age people = [Person("Alice", 30), Person("Bob", 25), Person("Eve", 35)] sorted_people = sorted(people, key=lambda p: p.age) #Sorting a List in Python using for loop for person in sorted_people: print(f"{person.name} is {person.age} years old")
Output:
Bob is 25 years old Alice is 30 years old Eve is 35 years old
The sorted() method in Python can also be used to reverse the order. This means by default the sorted method arranges the elements in ascending order but we can change the setting by using the reverse argument.
For example see the example below:
# Sort a list of numbers in descending order numbers = [3, 1, 4, 2] #Sorting a List in Python sorted_numbers = sorted(numbers, reverse=True) print(sorted_numbers) # [4, 3, 2, 1]
We can also use the sorted() method on a list of tuples to arrange them in proper order.
# Sort a list of tuples by the second element in each tuple tuples = [(1, 3), (2, 1), (3, 4), (4, 2)] #Sorting a List in Python sorted_tuples = sorted(tuples, key=lambda t: t[1]) #printing print(sorted_tuples) # [(2, 1), (4, 2), (1, 3), (3, 4)]
I hope these examples help you understand how to use the sorted() function in Python! Let me know if you have any questions.
Sorting a List in Python using the Sort Method
Certainly! The sort() method is a built-in function in Python that can be used to sort a list in ascending order. It modifies the original list in place, so you don’t have to create a new list to store the sorted elements.
Here is an example of how to use the sort() method to sort a list of integers in ascending order:
#creating a list numbers = [3, 1, 4, 2] #Sorting a List in Python numbers.sort() #print print(numbers) # Output: [1, 2, 3, 4]
You can also use the sort() method to sort a list of strings in alphabetical order:
#creating a list words = ['apple', 'banana', 'cherry', 'date'] # Sorting a List in Python words.sort() #printing print(words) # Output: ['apple', 'banana', 'cherry', 'date']
You can specify the reverse parameter as True to sort the list in descending order:
numbers = [3, 1, 4, 2] #Sorting a List in Python numbers.sort(reverse=True) #output print(numbers) # Output: [4, 3, 2, 1] #creating a list words = ['apple', 'banana', 'cherry', 'date'] #Sorting a List in Python words.sort(reverse=True) #printing print(words) # Output: ['date', 'cherry', 'banana', 'apple']
You can also use the sorted() function to sort a list in ascending order, but it returns a new list with the sorted elements rather than modifying the original list. Here is an example:
#creating a list numbers = [3, 1, 4, 2] #Sorting a List in Python sorted_numbers = sorted(numbers) #printing print(sorted_numbers) # Output: [1, 2, 3, 4] print(numbers) # Output: [3, 1, 4, 2]
Summary
In Python, the sort method is a list method that modifies the list in place, sorting its elements in ascending order. The sorted function, on the other hand, returns a new sorted list from the elements of a given iterable. It does not modify the original iterable. Both sort and sorted accept an optional key parameter, which allows you to specify a function that returns a value to use as the sort key. They also accept an optional reverse parameter, which can be set to True to sort the elements in descending order. In this short article, we learned how we can use the sort() and sorted() method to sort a list in Python using various examples.
Also read: