ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.