Sort a List of Tuples by Second Item - Python Last Updated : 18 Feb, 2025 Comments Improve Suggest changes Like Article Like Report The task of sorting a list of tuples by the second item is common when working with structured data in Python. Tuples are used to store ordered collections and sometimes, we need to sort them based on a specific element, such as the second item. For example, given the list [(1, 3), (4, 1), (2, 2)], the goal is to reorder it by the second item, resulting in [(4, 1), (2, 2), (1, 3)].Using sorted()sorted() is the most versatile way to sort data. It takes an iterable and returns a new sorted list without modifying the original list. We can specify the sorting key using the key parameter which allows us to sort the tuples based on the second item. Python a = [(1, 3), (4, 1), (2, 2)] # Sort the list based on the second element res = sorted(a, key=lambda x: x[1]) print(res) Output[(4, 1), (2, 2), (1, 3)] Explanation: sorted(a, key=lambda x: x[1]) extract the second element (x[1]) from each tuple for comparison and list is returned in sorted order by the second item of each tuple.Table of ContentUsing sort()Using itemgetterUsing sort()Unlike sorted(), the sort() method sorts the list in place meaning it directly modifies the original list without returning a new one. This method is more efficient when we do not need to preserve the original list order. Python a = [(5, 2), (1, 6), (3, 4)] # Sort the list in-place based on the second element a.sort(key=lambda x: x[1]) print(a) Output[(5, 2), (3, 4), (1, 6)] Explanation: a.sort(key=lambda x: x[1]) sorts the list a in place based on the second item of each tuple and result is directly stored in the original list a and no new list is created.Using itemgetteritemgetter() from the operator module provides a more efficient and readable way to retrieve the sorting key. It is often faster than using a lambda function, especially for larger datasets. Python from operator import itemgetter a = [(7, 5), (3, 8), (2, 6)] # Sort the list based on the second element res = sorted(a, key=itemgetter(1)) print(res) Output[(7, 5), (2, 6), (3, 8)] Explanation: itemgetter(1) retrieves the second item (1) from each tuple. It is more efficient than using a lambda function and sorted() sorts the list based on the second item of each tuple. Comment More infoAdvertise with us Next Article Sort a List of Tuples by Second Item - Python S Shivam_k Follow Improve Article Tags : Python Python Programs Computer Science Fundamentals DSA python-list python-tuple Python list-programs Python tuple-programs +4 More Practice Tags : pythonpython-list Similar Reads Python - Sort by Frequency of second element in Tuple List Given list of tuples, sort by frequency of second element of tuple. Input : test_list = [(6, 5), (1, 7), (2, 5), (8, 7), (9, 8), (3, 7)] Output : [(1, 7), (8, 7), (3, 7), (6, 5), (2, 5), (9, 8)] Explanation : 7 occurs 3 times as 2nd element, hence all tuples with 7, are aligned first. Input : test_l 6 min read How To Slice A List Of Tuples In Python? In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i 3 min read Sort Tuple of Lists in Python The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [ 3 min read Sort the values of first list using second list in Python Sorting the values of the first list using the second list in Python is commonly needed in applications involving data alignment or dependency sorting. In this article, we will explore different methods to Sort the values of the first list using the second list in Python.Using zip() and sorted() zip 3 min read Custom Sorting in List of Tuples - Python The task of custom sorting in a list of tuples often involves sorting based on multiple criteria. A common example is sorting by the first element in descending order and the second element in ascending order. For example, given a = [(7, 8), (5, 6), (7, 5), (10, 4), (10, 1)], sorting the tuples by t 3 min read Python - Sort Tuple List by Nth Element of Tuple We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)]Using sorted() with lambdasorted() function wit 3 min read Python | Sort lists in tuple Sometimes, while working with Python tuples, we can have a problem in which we need to sort the tuples which constitutes of lists and we need to sort each of them. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + sorted() + generator expression This task ca 5 min read Python - Sort list of lists by the size of sublists The problem is to sort a list of lists based on the size of each sublist. For example, given the list [[1, 2], [1], [1, 2, 3]], the sorted result should be [[1], [1, 2], [1, 2, 3]] when sorted in ascending order. We will explore multiple methods to do this in Python.Using sorted() with len() This me 3 min read Python | Reverse each tuple in a list of tuples Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega 5 min read Like