Python program to find the maximum element in tuple list

In this program, we are given a list of tuples. We need to create a Python program to find the maximum element in tuple list.
Submitted by Shivang Yadav, on December 24, 2021

While working with collections in Python in higher-level implications like machine learning or data structure, we need to find elements based on certain conditions as required by the program. Here, we will be creating a Python program to find the maximum element in the tuple list.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Finding the maximum element in tuple list

In this article, we are given a list of tuples. We need to create a Python program to find the maximum element in the tuple list.

Input: 
[(4, 1, 6), (2, 3, 9), (12, 7, 5)]

Output:
12

We need to iterate through all the elements of the tuple list and find the maximum value from all of them which is our required value.

Method 1:

One method to find the maximum element of the tuple list is by iterating over the list of tuples using a generator expression and then finding the maximum value from the resultant collection/iterator using the max() method.

# Python program to find the 
# maximum element in tuple list
  
# Initializing and printing 
# the list of tuples 
tupList = [(4, 1, 6), (2, 3, 9), (12, 7, 5)]
print("The element of list of tuples are " + str(tupList))
  
# Finding Maximum elements 
# in Tuple list 
maxVal = max(int(j) for i in tupList for j in i)
print("The Maximum value from the list of tuples is : " + str(maxVal))

Output:

The element of list of tuples are [(4, 1, 6), (2, 3, 9), (12, 7, 5)]
The Maximum value from the list of tuples is : 12

Method 2:

Another method to solve the problem is by flattening the list of tuples using the from_iterable() method from the chain library. Then, we will find the maximum value from the created the map() using the max() method.

# Python program to find the 
# maximum element in tuple list
  
from itertools import chain   
  
# Initializing and printing 
# the list of tuples 
tupList = [(4, 1, 6), (2, 3, 9), (12, 7, 5)]
print("The element of list of tuples are " + str(tupList))
  
# Finding Maximum elements 
# in Tuple list 
maxVal = max(map(int, chain.from_iterable(tupList)))
print("The Maximum value from the list of tuples is : " + str(maxVal))

Output:

The element of list of tuples are [(4, 1, 6), (2, 3, 9), (12, 7, 5)]
The Maximum value from the list of tuples is : 12

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.