
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Maximum value in record list as tuple attribute in Python
When it is required to find the maximum value in a record list of tuples, the list comprehension and the 'max' methods can be used.
A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on).
A list of tuple basically contains tuples enclosed in a list. The list comprehension is a shorthand to iterate through the list and perform operations on it.
The 'max' method can be used to find the maximum of all the elements in an iterable.
Below is a demonstration of the same −
Example
my_list = [('Will', [67, 45, 89]), ('Jam', [34, 56,13]), ('Pow', [99, 123, 89]), ('Nyk', [0, 56, 5])] print ("The list of tuples is : " ) print(my_list) my_result = [(key, max(lst)) for key, lst in my_list] print ("The maximum of list tuple attribute is : " ) print(my_result)
Output
The list of tuples is : [('Will', [67, 45, 89]), ('Jam', [34, 56, 13]), ('Pow', [99, 123, 89]), ('Nyk', [0, 56, 5])] The maximum of list tuple attribute is : [('Will', 89), ('Jam', 56), ('Pow', 123), ('Nyk', 56)]
Explanation
- A list of tuples is defined, and is displayed on the console.
- The list comprehension is used to iterate through the list, and the maximum of the values in the tuple (inside the list of tuple) is obtained.
- This operation's result is assigned to a variable.
- It is then displayed on the console as the output.
- Related Articles
- Summation of list as tuple attribute in Python
- Maximum element in tuple list in Python
- Record Similar tuple occurrences in Python
- Get maximum of Nth column from tuple list in Python
- Python - Unique keys count for Value in Tuple List
- Python Group by matching second tuple value in list of tuples
- Python program to unique keys count for Value in Tuple List
- Flatten tuple of List to tuple in Python
- Index minimum value Record in Python
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- Python – Cross Pairing in Tuple List
- Why python returns tuple in list instead of list in list?
- Find Maximum difference between tuple pairs in Python
- Find the sublist with maximum value in given nested list in Python
- Get first element with maximum value in list of tuples in Python

Advertisements