Built with Tutify.io

    AIStudyLabs

    How to Sort List of Tuples in Python

    How to Sort List of Tuples in Python

    How to sort a list of tuples in python? Tuples are a fundamental data structure in Python that allows you to store multiple values in a single object. A tuple is an ordered and immutable (cannot update elements in a tuple) collection of items.

    Tuples in Python are similar to lists but they cannot be changed. This makes them useful in certain situations. The immutability of tuples makes them ideal for certain tasks and scenarios. In this article, I will explain how to sort a list of tuples in Python by using sort() method and sorted() function with examples.

    Methods to Sort List of Tuples in Python

    1. By using list sort() method
    2. By using built-in sorted() function
    3. Using these Sort the list of tuples in ascending order
    4. Sort the list of tuples in descending order
    5. Sort by second element of the tuple
    6. Sort by Multiple tuple elements

    <a id="h-1-quick-examples-of-sort-a-list-of-tuples"></a>

    1. Quick Examples of Sort a List of Tuples

    If you are in a hurry, below are some quick examples of how to sort a list of tuples in python.

    
    # Quick examples of sort a list of tuples
    
    # Example 1: Sort list of tuples 
    # using list.sort()
    sort_tuples = [(3, 7), (2, 12), (5, 10), (9, 0)]
    sort_tuples.sort(key=lambda x: x[1])
    
    # Example 2: Sort list of tuples 
    # using sorted()
    tuples = [(3, 7), (2, 12), (5, 10), (9, 0)]
    sorted_tuples = sorted(tuples)
    
    # Example 3: Sort the list of tuples by first element descending
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    tuples.sort(key=lambda x: x[0], reverse=True)
    
    # Example 4: Sorted the list of tuples by first element descending
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    sorted_tuples = sorted(tuples, key=lambda x: x[0], reverse=True)
    
    # Example 5: Sorted the list of tuples by second element
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    sorted_list = sorted(tuples, key=lambda x: x[1])
    
    # Example 6: Sort the list of tuples by second element
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    tuples.sort(key=lambda x: x[1])
    
    # Example 7: Sort list of tuples by length
    tuples= [('Hyperion', 3500), ('Hadoop', 2500),('Spark', 2200), ('Python', 3000)]
    tuples.sort(key=lambda x: len(x[0]))
    
    # Example 8: Sorted list of tuples by last element
    tuples = [('Hyperion', 3500), ('Hadoop', 2500),('Spark', 2200), ('Python', 3000)]
    tuples = sorted(tuples, key=lambda x: x[-1])
    
    # Example 9: Sort list of tuples by last element
    tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
    tuples.sort(key=lambda x: x[-1])
    
    # Example 10: Sort list of tuples by multiple elements
    tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
    sorted_list = sorted(tuples, key=lambda x: (x[0], x[1]))
    

    2. Sort the List of Tuples in Python

    You can sort a list of tuples in Python by using the sort() method, to specify the criteria for sorting, you can use the key parameter with value as lambda function that calculates the value used for sorting. Note that this method updates the original list with the sorted elements.

    
    # Create input tuple
    sort_tuples = [(3, 7), (2, 12), (5, 10), (9, 0)]
    print("Original list of tuples:",sort_tuples)
    
    # Sort list of tuples using sort()
    sort_tuples.sort(key=lambda x: x[1])
    print("Sorted list of tuples:",sort_tuples)
    

    Yields below output.

    Here, the elements are sorted by the second position of the tuple.

    You can also use built-in function sorted() to sort a list of tuples, this doesn't update the original list instead it returns the new list with the elements sorted in a specified order.

    
    # Sort list of tuples using sorted()
    tuples = [(3, 7), (2, 12), (5, 10), (9, 0)]
    sorted_tuples = sorted(tuples)
    print(sorted_tuples)
    
    # Output:
    # [(2, 12), (3, 7), (5, 10), (9, 0)]
    

    Here, by default tuple elements are sorted by the first position.

    <a id="h-3-sort-list-of-tuples-by-descending"></a>

    3. Sort List of Tuples by Descending

    You can sort a list of tuples in descending order by the first element by using the reverse parameter of the sorted() or the sort() and specifying the index to sort by using the key parameter. For example, the reverse parameter is set to True to sort the list in descending order, and the key parameter is set to lambda x:x[0] to sort by the first element of each tuple (index 0).

    
    # Create list of tuples
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    print("Orginal: ",tuples)
    
    # Sort list of tuples by descending
    tuples.sort(key=lambda x: x[0], reverse=True)
    print("Sorted: ",tuples)
    

    This example yields the below output.

    Similarly, to sort a list of tuples in descending order, you can use the sorted() function with the reverse parameter set to True.

    
    # Using sorted() function
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    print("Orginal: ",tuples)
    
    sorted_tuples = sorted(tuples, key=lambda x: x[0], reverse=True)
    print("Sorted: ",sorted_tuples)
    

    Yields the same output as above.

    <a id="h-4-sort-list-of-tuples-by-second-element"></a>

    4. Sort List of Tuples by Second Element

    You can also sort a list of tuples by the second element using sorted() and sort(). To sort by the second element use the lambda expression x[1] for key argument. Both methods sort the tuples based on the values returned by the key function, which in this case are the second element of each tuple.

    
    # Sort the list of tuples by second element
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    tuples.sort(key=lambda x: x[1])
    print(tuples)
    
    # Sorted the list of tuples by second element
    tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
    sorted_list = sorted(tuples, key=lambda x: x[1])
    print(sorted_list)
    
    # Output:
    # [(2500, 'Hadoop'), (3000, 'Python'), (2200, 'Spark')]
    

    <a id="h-5-sort-list-of-tuples-by-length"></a>

    5. Sort List of Tuples by Length

    You can also sort a list of tuples by the length of the element using the sort() method in Python. Here, the sort() method is used with the lambda expression that gets the length of value. Here the length is calculated and the element is sorted in ascending order by its first element.

    
    # Sort list of tuples by length
    tuples= [('Hyperion', 3500), ('Hadoop', 2500),('Spark', 2200), ('Python', 3000)]
    tuples.sort(key=lambda x: len(x[0]))
    print(tuples)
    
    # Output
    # [('Spark', 2200), ('Hadoop', 2500), ('Python', 3000), ('Hyperion', 3500)]
    

    Similarly, you can also use the sorted().

    <a id="h-6-sort-list-of-tuples-by-last-element"></a>

    6. Sort List of Tuples by Last Element

    Let's sort by the last element of the tuple in the Python list. To get the last lement use the x[-1] as an expression to the lambda. -1 refers the last indexes.

    
    # Sorted list of tuples by last element
    tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
    tuples.sort(key=lambda x: x[-1])
    print(tuples)
    
    # Output
    # [(2200, 'Hadoop'), (3000, 'Python'), (2500, 'Spark')]
    

    Similarly, you can also use the sorted().

    7. Sort List of Tuples by Multiple Elements

    To sort the list of tuples by multiple elements in Python, use the lambda expression with the elements you wanted. For instance, the key function lambdax:(x[0],x[1]) returns a tuple of the first and second elements of each tuple, and the sorted() function sorts the list of tuples based on these tuples.

    
    # Sort list of tuples by multiple elements
    tuples = [(2500, 'Spark'), (2200, 'Hadoop'), (3000, 'Python')]
    sorted_list = sorted(tuples, key=lambda x: (x[0], x[1]))
    print(sorted_list)
    
    # Output
    # [(2200, 'Hadoop'), (2500, 'Spark'), (3000, 'Python')]
    

    <a id="h-frequently-asked-questions-on-sort-list-of-tuples-in-python"></a>

    Frequently Asked Questions on Sort List of Tuples in Python

    To sort a list of tuples in ascending order based on a specific element of each tuple, you can use the sorted() function with the key parameter

    To sort a list of tuples in descending order based on a specific element of each tuple, you can use the sorted() function with the key parameter and set reverse=True.

    You can. Use the sorted() function with a custom key function that calculates the sum of tuple elements.

    It is possible to sort a list of tuples without modifying the original list. The sorted() function returns a new sorted list without modifying the original list.

    You can specify a tuple of sorting keys using the key parameter. For example, to sort based on the first element in descending order and then on the second element in ascending order.

    Conclusion

    In this article, I have explained how to sort a list of tuples in Python by using the list sort() method and built-in sorted() function with examples. By using these we sorted the list of tuples in ascending order, descending order, by first, and last elements, and finally by multiple elements.

    Happy Learning !!

    Related Articles

    References