Python Program to Sort the list according to the column using lambda
Last Updated :
16 Dec, 2024
We often need to organize data based on a particular value, such as sorting rows in a dataset by a column. Python makes this task simple by using the sorted()
function along with a lambda
function. In this article, we will explore various approaches to Sorting the list according to column.
Let's understand this with a basic example:
Python
a = [(2, 'B'), (1, 'A'), (3, 'C')]
# Sort by the first column
b = sorted(a, key=lambda x: x[0])
print(b)
Output[(1, 'A'), (2, 'B'), (3, 'C')]
Explanation:
lambda x: x[0]
selects the first element of each tuple to sort by.- The list is sorted in ascending order based on the first column.
Sort by First Column
To sort a list of lists or tuples by the first column (index 0), we can use the sorted() function with a lambda function as the sorting key.
Python
a = [(3, 'fun!'), (1, 'Python'), (2, 'is')]
# Sort by first column (index 0)
sorted_data = sorted(a, key=lambda x: x[0])
print(sorted_data)
Output[(1, 'Python'), (2, 'is'), (3, 'fun!')]
Explanation:
x[0]
extracts the first element of each tuple for sorting.- The list is sorted in ascending order by default.
Sort by Second Column
To sort a list of lists or tuples by the second column (index 1), we can use the sorted() function with a lambda function specifying the second element.
Python
a = [(3, 'fun!'), (1, 'Python'), (2, 'is')]
# Sort by second column (index 1)
sorted_data = sorted(a, key=lambda x: x[1])
print(sorted_data)
Output[(1, 'Python'), (3, 'fun!'), (2, 'is')]
Sort a List of Lists by a Specific Column
To sort a list of lists by a specific column (index) in Python, we can use the sorted()
function and specify the column index in the key
parameter using a lambda function.
Python
a = [[3, 50], [1, 70], [2, 60]]
# Sort by second column (index 1)
sorted_data = sorted(a, key=lambda x: x[1])
print(sorted_data)
Output[[3, 50], [2, 60], [1, 70]]
Explanation:
- The
lambda x: x[1]
extracts the second column values. - The list is sorted numerically by the second column.
Sort a list of tuple in Descending Order
We can use the sorted() function with the reverse=True parameter, and specify the column (or element) by which we want to sort using the key parameter (if necessary).
Python
a = [(3, 'fun!'), (1, 'Python'), (2, 'is')]
# Sort by first column in descending order
sorted_data = sorted(a, key=lambda x: x[0], reverse=True)
print(sorted_data)
Output[(3, 'fun!'), (2, 'is'), (1, 'Python')]
Explanation:
- The
reverse=True
argument sorts the data in descending order.
Similar Reads
Python - Sort list according to other list order Sorting a list based on the order defined by another list is a common requirement in Python, especially in scenarios involving custom sorting logic. This ensures that elements in the first list appear in the exact order specified in the second list. Python provides various methods to accomplish this
2 min read
Python program to Sort a List of Strings by the Number of Unique Characters Given a list of strings. The task is to sort the list of strings by the number of unique characters. Examples: Input : test_list = ['gfg', 'best', 'for', 'geeks'], Output : ['gfg', 'for', 'best', 'geeks'] Explanation : 2, 3, 4, 4 are unique elements in lists. Input : test_list = ['gfg', 'for', 'geek
6 min read
Python Program to Sort Matrix Rows According to Primary and Secondary Indices Given Matrix, the task here is to write a Python program to sort rows based on primary and secondary indices. First using primary indices the rows will be arranged based on the element each row has at the specified primary index. Now if two rows have the same element at the given primary index, sort
3 min read
Sort List of Lists Ascending and then Descending in Python Sorting a list of lists in Python can be done in many ways, we will explore the methods to achieve the same in this article.Using sorted() with a keysorted() function is a flexible and efficient way to sort lists. It creates a new list while leaving the original unchanged.Pythona = [[1, 2, 3], [3, 2
3 min read
Python Program to Sort Words in Alphabetical Order The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() m
2 min read
Convert List of String into Sorted List of Integer - Python We are given a list of string a = ['3', '1', '4', '1', '5'] we need to convert all the given string data inside the list into int and sort them into ascending order so the output list becomes a = [1, 1, 3, 4, 5]. This can be achieved by first converting each string to an integer and then sorting the
3 min read