Open In App

Python – Get maximum of Nth column from tuple list

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Initialize a list of tuples called test_list containing 3 tuples.
  2. Print the original list.
  3. Initialize an integer variable N to 2.
  4. Use list comprehension to extract the Nth element from each tuple in test_list, creating a list of the values in the Nth column.
  5. Use the max() function to find the maximum value in the list from step 4.
  6. Print the result as the maximum value of the Nth column of the tuple list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Nth column Maximum in tuple list
# using list comprehension + max()
 
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Maximum in tuple list
# using list comprehension + max()
res = max([sub[N] for sub in test_list])
 
# printing result
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:

  1. Import necessary modules: itemgetter from operator module and imap from itertools module.
  2. Initialize the list of tuples, test_list.
  3. Print the original list using print() function and string concatenation.
  4. Initialize the value of N, which represents the column number.
  5. Use itemgetter() to get the N-th element from each tuple.
  6. Use imap() to apply the itemgetter() function to each tuple in test_list.
  7. Use max() to find the maximum value among the N-th elements of all tuples in test_list.
  8. Store the maximum value in res.
  9. Print the result using print() function and string concatenation.

Below is the implementation of the above approach:

Python3




# Python code to demonstrate working of
# Nth column Maximum in tuple list
# using imap() + max() + itemgetter()
from operator import itemgetter
from itertools import imap
 
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Maximum in tuple list
# using imap() + max() + itemgetter()
idx = itemgetter(N)
res = max(imap(idx, test_list))
 
# printing result
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:

  1. Create an empty list x to store the Nth element of each tuple.
  2. Iterate over each tuple i in test_list.
  3. Append the Nth element of tuple i to the list x.
  4. Sort the list x in ascending order.
  5. Get the last element of the sorted list x using x[-1] and store it in a variable res.
  6. 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




# Python3 code to demonstrate working of
# Nth column Maximum in tuple list
 
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
x=[]
for i in test_list:
    x.append(i[N])
x.sort()
res=x[-1]
# printing result
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




# Python3 code to demonstrate working of
# Nth column Maximum in tuple list
# using reduce() + lambda
  
# importing reduce from functools
from functools import reduce
  
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize N
N = 2
  
# Nth column Maximum in tuple list
# using reduce() + lambda
res = reduce(lambda x, y : x if x[N] > y[N] else y, test_list)[N]
  
# printing result
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 library
import numpy as np
 
# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
 
# initialize N
N = 2
 
# convert tuple list to numpy array
arr = np.array(test_list)
 
# find maximum of Nth column in tuple list using NumPy library
res = np.max(arr[:,N])
 
# printing result
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




# initialize list
test_list = [(5, 6, 7), (1, 3, 5), (8, 9, 19)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Nth column Maximum in tuple list using loop
max_val = test_list[0][N]
for tup in test_list:
    if tup[N] > max_val:
        max_val = tup[N]
 
# printing result
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
Previous
Next
Share your thoughts in the comments

Similar Reads