- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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)
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)]
- 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.
- 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 - Clearing list as dictionary value
- Python - Unique keys count for Value in Tuple List
- Display duplicate record as a distinct value with corresponding values as distinct comma separated list in MySQL?
- Index minimum value Record in Python
- Flatten tuple of List to tuple in Python
- Python Group by matching second tuple value in list of tuples
- Python program to unique keys count for Value in Tuple List
- Raise elements of tuple as power to another tuple in Python
- Why python returns tuple in list instead of list in list?
- Find Maximum difference between tuple pairs in Python
- List vs tuple vs dictionary in Python