- 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
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 −
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)
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)]
- 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.
- Maximum element in tuple list in Python
- Reverse each tuple in a list of tuples in Python
- How we can update a Python tuple element value?
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- How we can update a Python list element value?
- Flatten tuple of List to tuple in Python
- Python – Check if any list element is present in Tuple
- Python – Extract Kth element of every Nth tuple in List
- Adding K to each element in a Python list of integers
- Python – Test if tuple list has a single element
- Python Program to find the cube of each list element
- Get tuple element data types in Python
- Why python returns tuple in list instead of list in list?
- Python program to get the indices of each element of one list in another list