
- 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
Update each element in tuple list in Python
When it is required to update every element in a tuple list (i.e list of tuples), list comprehension can be used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
Below is a demonstration of the same −
Example
my_list_1 = [(7, 8, 0), (3, 45, 3), (2, 22,4)] print ("The list of tuple is : " ) print(my_list_1) element_to_add = 41 my_result = [tuple(j + element_to_add for j in sub ) for sub in my_list_1] print("List of tuple after update is : ") print(my_result)
Output
The list of tuple is : [(7, 8, 0), (3, 45, 3), (2, 22, 4)] List of tuple after update is : [(48, 49, 41), (44, 86, 44), (43, 63, 45)]
Explanation
- A list of tuple is defined, and is displayed on the console.
- An element that needs to be added to the list of tuple is defined.
- This list of tuple is iterated over, and the element is added to every tuple in the list of tuples.
- This result is assigned to a value.
- It is displayed as output on the console.
- Related Articles
- Maximum element in tuple list in Python
- How we can update a Python tuple element value?
- Reverse each tuple in a list of tuples in Python
- Python – Check if any list element is present in Tuple
- Python – Extract Kth element of every Nth tuple in List
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Flatten tuple of List to tuple in Python
- Python – Test if tuple list has a single element
- How we can update a Python list element value?
- Python – Cross Pairing in Tuple List
- Why python returns tuple in list instead of list in list?
- Get tuple element data types in Python
- Difference Between List and Tuple in Python
- Grouped summation of tuple list in Python

Advertisements