Q:

Python program to extract 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

We are given a list of tuples with each tuple with values stored as records. We need to create a python program to extract the maximum value in the record list as a tuple attribute.

Input:
[('java', [4, 3, 8, 9]), ('python', [7, 1, 4, 2]), ('scala' , [4, 5, 6, 11])]

Output:
[('java', 9), ('python', 7), ('scala', 11)]

To extract the maximum value in the record, we will be traversing each tuple record and simply finding the largest value from the record.

All Answers

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

Method 1:

One combination of methods in python that can perform the task is finding the maximum of each record using the max() method and extracting it using list comprehension by iterating the list of tuples.

# Python program to extract maximum value 
# in record list as record 

# Initializing and printing list of tuples
tupList = [('java', [4, 3, 8, 9]), ('python', [7, 1, 4, 2]), ('scala' , [4, 5, 6, 11])]
print("The elements of list of  tuples are " + str(tupList))

# Extracting maximum value from each record 
# using max() method with list comphrehension 
maxList = [(key, max(rec)) for key, rec in tupList]

# Printing maximum record value list 
print("The extracted values from the list of tuple are " + str(maxList))

Output:

The elements of list of  tuples are [('java', [4, 3, 8, 9]), ('python', [7, 1, 4, 2]), ('scala', [4, 5, 6, 11])]
The extracted values from the list of tuple are [('java', 9), ('python', 7), ('scala', 11)]

Method 2:

One more combination that can be employed is mapping the list which uses a lambda function to extract tuple with value and maximum of record extracted using the the max() method.

# Python program to extract maximum value 
# in record list as record 

# Initializing and printing list of tuples
tupList = [('java', [4, 3, 8, 9]), ('python', [7, 1, 4, 2]), ('scala' , [4, 5, 6, 11])]
print("The elements of list of  tuples are " + str(tupList))

# Extracting maximum value from each record 
# using max() method with list comphrehension 
maxList = list(map(lambda tup: (tup[0], max(tup[1])), tupList))

# Printing maximum record value list 
print("The extracted values from the list of tuple are " + str(maxList))

Output:

The elements of list of  tuples are [('java', [4, 3, 8, 9]), ('python', [7, 1, 4, 2]), ('scala', [4, 5, 6, 11])]
The extracted values from the list of tuple are [('java', 9), ('python', 7), ('scala', 11)]

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 modulo of tuple elements... >>
<< Python program to perform concatenation of two str...