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 # 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))
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)
OutputThe 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 # 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))
OutputThe 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:
- 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)] # 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.
OutputThe 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.
Similar Reads
Python | Minimum element in tuple list
Sometimes, while working with data in form of records, we can have a problem in which we need to find the minimum 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 :
5 min read
Python - Maximum element in Cropped List
Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Python - K Maximum elements with Index in List
GIven a List, extract K Maximum elements with their indices. Input : test_list = [5, 3, 1, 4, 7, 8, 2], K = 2 Output : [(4, 7), (5, 8)] Explanation : 8 is maximum on index 5, 7 on 4th. Input : test_list = [5, 3, 1, 4, 7, 10, 2], K = 1 Output : [(5, 10)] Explanation : 10 is maximum on index 5. Method
4 min read
Python - Ranged Maximum Element in String List
Sometimes, while working with Python data, we can have a problem in which we have data in form of String List and we require to find the maximum element in that data, but that also in a certain range of indices. This is quite peculiar problem but can have application in data domains. Let's discuss c
5 min read
Python - Maximum frequency in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 min read
Python - Maximum and Minimum K elements in Tuple
Sometimes, while dealing with tuples, we can have problem in which we need to extract only extreme K elements, i.e maximum and minimum K elements in Tuple. This problem can have applications across domains such as web development and Data Science. Let's discuss certain ways in which this problem can
8 min read
Python - Maximum of K element in other list
Given two lists, extract maximum of elements with similar K in corresponding list. Input : test_list1 = [4, 3, 6, 2, 8], test_list2 = [3, 6, 3, 4, 3], K = 3 Output : 8 Explanation : Elements corresponding to 3 are, 4, 6, and 8, Max. is 8. Input : test_list1 = [10, 3, 6, 2, 8], test_list2 = [5, 6, 5,
4 min read
Python - Elements Maximum till current index in List
Given list with elements, extract element if it's the maximum element till current index. Input : test_list = [4, 6, 7, 8] Output : [4, 6, 7, 8] Explanation : All elements are maximum till their index. Input : test_list = [6, 7, 3, 6, 8, 7] Output : [7, 8] Explanation : 7 and 8 are maximum till thei
7 min read
Python | Positions of maximum element in list
Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python - Maximum element till K value
One of the problem that is basically a subproblem for many complex problems, finding maximum number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this
3 min read