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

In this program, we are given a list of tuples with each tuple consisting of a record. We need to create a Python program to extract the maximum value in the record list as a tuple attribute.
Submitted by Shivang Yadav, on December 12, 2021

Storing a large amount of complex data requires working with a mixture of complex collections nested to make sure the data is stored in such a way that it can later be extracted based on the user's requirement. If required, we can extract data from the nested collection based on maximum value or any other factor. In this program, we will be creating a program that will extract maximum value from the records list with has multiple tuples with record data.

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

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.

Let's see different implementations of this approach using combinations of different python functions.

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

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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