Python | Maximum element in tuple list
Last Updated :
15 Mar, 2023
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
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = max ( int (j) for i in test_list for j in i)
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
from itertools import chain
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = max ( map ( int , chain.from_iterable(test_list)))
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))
|
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))
|
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))
|
Method #7: Using extend() and max() methods
Python3
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
x = []
for i in test_list:
x.extend( list (i))
res = max (x)
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
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
res = np. max (test_list)
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 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:
- Initialize a list of tuples.
- Flatten the list of tuples into a single list of integers using reduce.
- Remove any 0 or False values from the flattened list using filter.
- Find the maximum value in the filtered list using max.
- Print the maximum value.
Python3
from functools import reduce
test_list = [( 2 , 4 ), ( 6 , 7 ), ( 5 , 1 ), ( 6 , 10 ), ( 8 , 7 )]
print ( "The original list : " + str (test_list))
flat_list = reduce ( lambda acc, x: acc + list (x), test_list, [])
filtered_list = list ( filter ( lambda x: x, flat_list))
res = max (filtered_list)
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
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.
Share your thoughts in the comments
Please Login to comment...