Open In App

Python | Maximum element in tuple list

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using max() + generator expression This is the most basic method to achieve the solution to this task. In this, we iterate over whole nested lists using generator expression and get the maximum element using max(). 

Python3




# Python3 code to demonstrate working of
# Maximum element in tuple list
# using max() + generator expression
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Maximum element in tuple list
# using max() + generator expression
res = max(int(j) for i in test_list for j in i)
 
# printing result
print("The Maximum element of list is : " + str(res))


Output : 

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

Method #2 : Using max() + map() + chain.from_iterable() The combination of above methods can also be used to perform this task. In this, the extension of finding maximum is done by combination of map() and from_iterable(). 

Python3




# Python3 code to demonstrate working of
# Maximum element in tuple list
# using max() + map() + chain.from_iterable()
from itertools import chain
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Maximum element in tuple list
# using max() + map() + chain.from_iterable()
res = max(map(int, chain.from_iterable(test_list)))
 
# printing result
print("The Maximum element of list is : " + str(res))


Output : 

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

Method #3: Using list comprehension method 

Python3




test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
x=[j for i in test_list for j in i ]
print(max(x))


Output

10

Method #4: Using enumerate function

Python3




test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
x=[j for a,i in enumerate(test_list) for j in i ]
print(max(x))


Output

10

Method #5: Using itemgetter

Python3




from operator import itemgetter
my_list = [('Will', 23), ('Jane', 21), ('El', 24), ('Min', 101)]
 
 
my_result = max(my_list, key = itemgetter(1))[1]
 
print ("The maximum value is : ")
print(my_result)


Output

The maximum value is : 
101

Method #6: Using the lambda function

Python3




test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
x=[j for i in test_list for j in i ]
x=list(filter(lambda i: (i),x))
print(max(x))


Output

10

Method #7:  Using extend() and max() methods

Python3




# Python3 code to demonstrate working of
# Maximum element in tuple list
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Maximum element in tuple list
x=[]
for i in test_list:
    x.extend(list(i))
res=max(x)
 
# printing result
print("The Maximum element of list is : " + str(res))


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

Method #8: Using numpy.max()

Note: Install numpy module using command “pip install numpy”

You can use numpy.max() to find the maximum element in the tuple list.

Python3




import numpy as np
 
# initialize list
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
 
# Maximum element in tuple list
# using numpy.max()
res = np.max(test_list)
 
# printing result
print("The Maximum element of list is : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

method has the time complexity of O(n) where n is the number of elements in the list. Auxiliary Space is O(1)

Method #9:Using reduce

Algorithm:

  1. Initialize a list of tuples.
  2. Flatten the list of tuples into a single list of integers using reduce.
  3. Remove any 0 or False values from the flattened list using filter.
  4. Find the maximum value in the filtered list using max.
  5. Print the maximum value.

Python3




from functools import reduce
 
test_list = [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
 
# printing original list
print("The original list : " + str(test_list))
# Use reduce to flatten the list of tuples into a single list of integers
flat_list = reduce(lambda acc, x: acc + list(x), test_list, [])
 
# Use filter to remove any 0 or False values from the list
filtered_list = list(filter(lambda x: x, flat_list))
 
# Find the maximum value in the filtered list
res = max(filtered_list)
# printing result
print("The Maximum element of list is : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original list : [(2, 4), (6, 7), (5, 1), (6, 10), (8, 7)]
The Maximum element of list is : 10

Time complexity is O(n)

In the case of the provided code, the time complexity is O(n), where n is the total number of elements in the list of tuples. This is due to the fact that the most time-consuming operations in the code involve iterating through the list of tuples, flattening it using reduce, and filtering out any 0 or False values using filter. These operations take linear time as a function of the input size, and therefore contribute to the overall O(n) time complexity. 

 space complexity is also O(n)

where n is the total number of elements in the list of tuples. This is due to the fact that the code requires a certain amount of memory to store the list of tuples, flatten it using reduce, and filter out any 0 or False values using filter. These operations require memory proportional to the size of the input, and therefore contribute to the overall O(n) space complexity.



Last Updated : 15 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads