Python – Get maximum of Nth column from tuple list
Last Updated :
06 Apr, 2023
Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Let’s discuss certain ways in which this task can be performed.
Method #1: Using list comprehension + max() This task can be performed using the combination of above functionalities. In this, the maximum index occurs using max() and list comprehension drives the iteration and access of Nth index element of each tuple in list.
Follow the below steps to implement the above idea:
- Initialize a list of tuples called test_list containing 3 tuples.
- Print the original list.
- Initialize an integer variable N to 2.
- Use list comprehension to extract the Nth element from each tuple in test_list, creating a list of the values in the Nth column.
- Use the max() function to find the maximum value in the list from step 4.
- Print the result as the maximum value of the Nth column of the tuple list.
Below is the implementation of the above approach:
Python3
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
N = 2
res = max ([sub[N] for sub in test_list])
print ( "Maximum of Nth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Maximum of Nth Column of Tuple List : 19
Time Complexity: O(M*N), where M is the length of the list and N is the number of elements at the Nth index of each tuple.
Auxiliary Space: O(1)
Method #2: Using imap() + max() + itemgetter() The combination of above functions can also achieve this task. This approach is generator based and recommended in case we have a very large list. In this, max() is used to perform maximum, itemgetter to get Nth index and imap() performs the task of mapping elements to extract max. Works only in Python2.
Step-by-step approach:
- Import necessary modules: itemgetter from operator module and imap from itertools module.
- Initialize the list of tuples, test_list.
- Print the original list using print() function and string concatenation.
- Initialize the value of N, which represents the column number.
- Use itemgetter() to get the N-th element from each tuple.
- Use imap() to apply the itemgetter() function to each tuple in test_list.
- Use max() to find the maximum value among the N-th elements of all tuples in test_list.
- Store the maximum value in res.
- Print the result using print() function and string concatenation.
Below is the implementation of the above approach:
Python3
from operator import itemgetter
from itertools import imap
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
N = 2
idx = itemgetter(N)
res = max (imap(idx, test_list))
print ( "Maximum of Nth Column of Tuple List : " + str (res))
|
Output :
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Maximum of Nth Column of Tuple List : 19
The time complexity of the given Python code is O(n), where n is the number of tuples in the list.
The auxiliary space of the given Python code is O(1)
Method #3: Using sort() method
Step-by-step approach:
- Create an empty list x to store the Nth element of each tuple.
- Iterate over each tuple i in test_list.
- Append the Nth element of tuple i to the list x.
- Sort the list x in ascending order.
- Get the last element of the sorted list x using x[-1] and store it in a variable res.
- Print the maximum of the Nth column of the tuple list by converting res to a string and concatenating it with the string “Maximum of Nth Column of Tuple List : ” using print().
Below is the implementation of the above approach:
Python3
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
N = 2
x = []
for i in test_list:
x.append(i[N])
x.sort()
res = x[ - 1 ]
print ( "Maximum of Nth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Maximum of Nth Column of Tuple List : 19
Time Complexity: O(M*N * log(N)), where M is the length of the list and N is the number of elements at the Nth index of each tuple.
Auxiliary Space: O(N)
Method #4: Using reduce() method
Python3
from functools import reduce
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
N = 2
res = reduce ( lambda x, y : x if x[N] > y[N] else y, test_list)[N]
print ( "Maximum of Nth Column of Tuple List : " + str (res))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Maximum of Nth Column of Tuple List : 19
Time Complexity: O(M*N), where M is the length of the list and N is the number of elements at the Nth index of each tuple.
Auxiliary Space: O(1)
Method 5 : Using NumPy library
In this method, we first convert the tuple list to a NumPy array using the np.array() function. Then, we use the np.max() function to find the maximum of the Nth column in the array.
Python3
import numpy as np
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
N = 2
arr = np.array(test_list)
res = np. max (arr[:,N])
print ( "Maximum of Nth Column of Tuple List : " + str (res))
|
OUTPUT:
Maximum of Nth Column of Tuple List : 19
Time complexity: The time complexity of this method is O(N), where N is the length of the input tuple list, as the NumPy library functions are highly optimized and efficient in terms of time complexity.
Auxiliary space: The auxiliary space used in this method is O(N), as we are creating a NumPy array of size N x 3 to store the input tuple list.
Method 6: Using a loop.
Step-by-step approach:
- Initialize the list of tuples.
- Initialize the value of N.
- Initialize a variable max_val to store the maximum value found in the Nth column so far.
- Loop through each tuple in the list.
- Get the Nth element from the tuple using indexing.
- Compare the Nth element with max_val.
- If it’s greater, update max_val to the Nth element.
- After the loop, max_val will contain the maximum value in the Nth column.
- Print the result.
Below is the implementation of the above approach:
Python3
test_list = [( 5 , 6 , 7 ), ( 1 , 3 , 5 ), ( 8 , 9 , 19 )]
print ( "The original list is : " + str (test_list))
N = 2
max_val = test_list[ 0 ][N]
for tup in test_list:
if tup[N] > max_val:
max_val = tup[N]
print ( "Maximum of Nth Column of Tuple List : " + str (max_val))
|
Output
The original list is : [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
Maximum of Nth Column of Tuple List : 19
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1) as only a constant amount of extra memory is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...