How to Sort List of Tuples in Python

Spread the love

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

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.


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

# Output
# [(9, 0), (3, 7), (5, 10), (2, 12)]

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.

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).


# Using sort()
tuples = [(2500, 'Hadoop'), (2200, 'Spark'), (3000, 'Python')]
print("Orginal: ",tuples)

tuples.sort(key=lambda x: x[0], reverse=True)
print("Sorted: ",tuples)

This example yields the below output.

python sort list tuples

Similarly, you can also write the same example using sorted()


# Using sorted()
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)

This yields the same output as above.

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')]

These examples yield the below output.

python sort list tuples

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().

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 example, 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')]

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

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing How to Sort List of Tuples in Python