Python | Sort Tuples in Increasing Order by any key
Last Updated :
14 Apr, 2023
Given a tuple, sort the list of tuples in increasing order by any key in tuple. Examples:
Input : tuple = [(2, 5), (1, 2), (4, 4), (2, 3)]
m = 0
Output : [(1, 2), (2, 3), (2, 5), (4, 4)]
Explanation: Sorted using the 0th index key.
Input : [(23, 45, 20), (25, 44, 39), (89, 40, 23)]
m = 2
Output : Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)]
Explanation: Sorted using the 2nd index key
Given tuples, we need to sort them according to any given key. This can be done using sorted() function where we sort them using key=last and store last as the key index according to which we have to sort the given tuples. Below is the Python implementation of the above approach:
Python
# Python code to sort a list of tuples
# according to given key.
# get the last key.
def last(n):
return n[m]
# function to sort the tuple
def sort(tuples):
# We pass used defined function last
# as a parameter.
return sorted(tuples, key = last)
# driver code
a = [(23, 45, 20), (25, 44, 39), (89, 40, 23)]
m = 2
print("Sorted:"),
print(sort(a))
Output:
Sorted: [(23, 45, 20), (89, 40, 23), (25, 44, 39)]
Another approach is using the operator.itemgetter() function from the operator module. The itemgetter() function returns a callable object that can be used to retrieve an item from an object, such as a tuple.
Here is an example of how to use itemgetter() to sort a list of tuples by any key:
Python3
import operator
def sort_tuples(tuples, key):
return sorted(tuples, key=operator.itemgetter(key))
tuples = [(2, 5), (1, 2), (4, 4), (2, 3)]
key = 0
print(sort_tuples(tuples, key)) # Output: [(1, 2), (2, 3), (2, 5), (4, 4)]
Output[(1, 2), (2, 5), (2, 3), (4, 4)]
This approach has the advantage of being concise and efficient, as it uses the built-in sorted() function and the itemgetter() function from the operator module. It is also easy to understand and implement.
Note that the itemgetter() function returns a callable object that can be used to retrieve an item from an object, such as a tuple. To sort the tuples, we pass this callable object to the key argument of the `sorted
Approach#3: Using lambda
One way to sort tuples in increasing order by any key is to use the sorted function and pass a key function that returns the desired element of each tuple.
Algorithm
1. Define a key function that returns the desired element of each tuple.
2. Use the sorted function to sort the list of tuples using the key function.
Python3
def sort_tuples(tuples, key_idx):
# Step 1: Define a key function that returns the desired element of each tuple.
key_func = lambda x: x[key_idx]
# Step 2: Use the `sorted` function to sort the list of tuples using the key function.
sorted_tuples = sorted(tuples, key=key_func)
return sorted_tuples
# Example usage:
tuples = [(2, 5), (1, 2), (4, 4), (2, 3)]
sorted_tuples = sort_tuples(tuples, 0)
print(sorted_tuples)
tuples = [(23, 45, 20), (25, 44, 39), (89, 40, 23)]
sorted_tuples = sort_tuples(tuples, 2)
print(sorted_tuples)
Output[(1, 2), (2, 5), (2, 3), (4, 4)]
[(23, 45, 20), (89, 40, 23), (25, 44, 39)]
Time Complexity: O(n log n), where n is the number of tuples in the list. This is because sorting takes O(n log n) time in the worst case.
Space Complexity: O(n), where n is the number of tuples in the list. This is because we create a new sorted list of tuples.
Similar Reads
Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat
2 min read
Sort Python Dictionary by Key or Value - Python There are two elements in a Python dictionary-keys and values. You can sort the dictionary by keys, values, or both. In this article, we will discuss the methods of sorting dictionaries by key or value using Python.Sorting Dictionary By Key Using sort()In this example, we will sort the dictionary by
6 min read
Python | Sort a tuple by its float element In this article, we will see how we can sort a tuple (consisting of float elements) using its float elements. Here we will see how to do this by using the built-in method sorted() and how can this be done using in place method of sorting. Examples: Input : tuple = [('lucky', '18.265'), ('nikhil', '1
3 min read
Python | sort list of tuple based on sum Given, a list of tuple, the task is to sort the list of tuples based on the sum of elements in the tuple. Examples: Input: [(4, 5), (2, 3), (6, 7), (2, 8)] Output: [(2, 3), (4, 5), (2, 8), (6, 7)] Input: [(3, 4), (7, 8), (6, 5)] Output: [(3, 4), (6, 5), (7, 8)] # Method 1: Using bubble sort Using th
4 min read
Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
3 min read