Python program to find maximum value in record list as tuple attribute

In this program, we are given a list of tuples where each tuple is a record consisting of an array of elements as attribute. We need to create a Python program to find the maximum value in the record list as tuple attribute.
Submitted by Shivang Yadav, on November 26, 2021

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)

Maximum value in record list as tuple attribute

To find the maximum value in the record list as tuple attribute, we will loop on the list of tuples and for each tuple find the maximum value amongst the elements of the array and return their pairs as tuples to result.

In Python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

Method 1:

A way to find the solution is by using list comprehension and to find the maximum values out of all elements of the array, we will use the max() method. This will iterate through each element of the array and find the maximum of the values of the array.

# Python program to find the Maximum value 
# in record list as tuple attribute

# initializing and printing the list of tuples
tupleList = [('scala', [7, 2, 9]), ('Java', [1, 5, 2]), ('Python', [9, 3, 1])]
print("The original list : " + str(tupleList))
  
# finding maximum value in record list as tuple attribute
maxList = [(key, max(recordList)) for key, recordList in tupleList]
  
# printing maximum tuple list
print("The list tuple attribute maximum is : " + str(maxList))

Output:

The original list : [('scala', [7, 2, 9]), ('Java', [1, 5, 2]), ('Python', [9, 3, 1])]
The list tuple attribute maximum is : [('scala', 9), ('Java', 5), ('Python', 9)]

Method 2:

Another method to solve the problem is by using a map() to map values passing the lambda function using the max() function to find the maximum of the record.

# Python program to find the Maximum value 
# in record list as tuple attribute

# initializing and printing the list of tuples
tupleList = [('scala', [7, 2, 9]), ('Java', [1, 5, 2]), ('Python', [9, 3, 1])]
print("The original list : " + str(tupleList))
  
# finding maximum value in record list as tuple attribute
maxList = list(map(lambda ele: (ele[0], max(ele[1])), tupleList))
  
# printing maximum tuple list
print("The list tuple attribute maximum is : " + str(maxList))

Output:

The original list : [('scala', [7, 2, 9]), ('Java', [1, 5, 2]), ('Python', [9, 3, 1])]
The list tuple attribute maximum is : [('scala', 9), ('Java', 5), ('Python', 9)]

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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