
- 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
Find the Maximum of Similar Indices in two list of Tuples in Python
If it is required to find the maximum of the similar indices in two list of tuples, the 'zip' method and list comprehension can be used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
The zip method takes iterables, aggregates them into a tuple, and returns it as the result.
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.
Below is a demonstration of the same −
Example
my_list_1 = [( 67, 45), (34, 56), (99, 123)] my_list_2 = [(10, 56), (45, 0), (100, 12)] print ("The first list is : " ) print(my_list_1) print ("The second list is : " ) print(my_list_2) my_result = [(max(x[0], y[0]), max(x[1], y[1])) for x, y in zip(my_list_1, my_list_2)] print("The maximum value among the two lists is :") print(my_result)
Output
The first list is : [(67, 45), (34, 56), (99, 123)] The second list is : [(10, 56), (45, 0), (100, 12)] The maximum value among the two lists is : [(67, 56), (45, 56), (100, 123)]
Explanation
- The two lists of tuples are defined, and are displayed on the console.
- The 'zip' method is used to combine both the list of tuples, and the 'max' method is used to fetch the maximum value among the tuples.
- This is converted to a list.
- This operation is assigned a variable.
- This variable is the output that is displayed on the console.
- Related Articles
- Combining tuples in list of tuples in Python
- Get first element with maximum value in list of tuples in Python
- Python - Ways to find indices of value in list
- Find elements of a list by indices in Python
- Find the tuples containing the given element from a list of tuples in Python
- Count tuples occurrence in list of tuples in Python
- Python program to find Tuples with positive elements in List of tuples
- Check if two list of tuples are identical in Python
- Remove duplicate tuples from list of tuples in Python
- Python program to find Tuples with positive elements in a List of tuples
- Convert list of tuples to list of list in Python
- Summation of tuples in list in Python
- Convert list of tuples into list in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python

Advertisements