How to Sort Almost Any Type of List in Python
Sorting lists is an essential programming technique for developers, as organizing data in a specific order is crucial in various applications. Python provides several built-in functions and libraries that make sorting lists easier and more efficient. In this article, we will cover some of the commonly used methods to sort almost any type of list in Python.
- Using the Sorted() Function:
The most straightforward method to sort a list in Python is by using the sorted() function. The sorted() function takes an iterable object such as a list, tuple or dictionary and returns a new sorted list, leaving the original object unchanged.
Here is an example:
“`
unsorted_list = [4, 1, 6, 8, 0, 3]
sorted_list = sorted(unsorted_list)
print(sorted_list)
Output: [0, 1, 3, 4, 6, 8]
“`
In this example, we have an unsorted list, i.e., “unsorted_list.” We then apply the sorted() function to it and store the result in a new variable “sorted_list”. The sorted function returns a new sorted list, i.e., [0, 1, 3, 4, 6, 8], which is printed on the console. The original list “unsorted_list” remains unchanged.
- Using the sort() Method:
Python also provides a built-in method for lists called sort(). The sort() method sorts the list in place, permanently changing the order of the elements.
Here is an example:
“`
unsorted_list = [4, 1, 2, 8, 0, 3]
unsorted_list.sort()
print(unsorted_list)
Output: [0, 1, 2, 3, 4, 8]
“`
In this example, we first define the unsorted list, i.e., “unsorted_list”. We then call the sort() method of the list object. The sort() method sorts the list in place and stores the result permanently. We then print the sorted list on the console.
- Sorting a List of Tuples:
In Python, sorting a list of tuples can be tricky as the tuples themselves may contain more than one value. The tuples are sorted based on the elements’ values starting from the first element. If two tuples have the same first element, then the second element is considered for sorting and so on.
Here’s an example:
“`
my_list = [(4, “apple”), (3, “banana”), (2, “orange”), (1, “grape”)]
my_list.sort()
print(my_list)
Output: [(1, ‘grape’), (2, ‘orange’), (3, ‘banana’), (4, ‘apple’)]
“`
In this example, we have a list of tuples “my_list.” Each tuple has two elements, an integer and a string. We call the sort() method of the list object, and the sorting happens based on the tuples’ first element. Hence, the output will be sorted by the integer value in the tuple.
- Sorting a List of Dictionaries:
Sorting a list of dictionaries is similar to sorting a list of tuples. In this case, the key value in the dictionaries is used to sort the list.
Here’s an example:
“`
my_list = [{“name”: “John”, “age”: 24}, {“name”: “Jane”, “age”: 32}, {“name”: “Bob”, “age”: 19}]
my_list.sort(key=lambda x: x[“age”])
print(my_list)
Output: [{‘name’: ‘Bob’, ‘age’: 19}, {‘name’: ‘John’, ‘age’: 24}, {‘name’: ‘Jane’, ‘age’: 32}]
“`
In this example, we have a list of dictionaries “my_list.” Each dictionary has two key-value pairs, “name” and “age”. We use a lambda function to sort the list based on the “age” key. The sorted list is then printed on the console.