Q:

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

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to find the index of minimum value ... >>
<< Python program to sort list of tuples alphabetical...