Example:
tuple = ("python", "includehelp", 43, 54.23)
Updating Each element in Tuple List
In this problem, we are given a list of tuples and an adder element to be added to each element of the list. Our program will be adding the element to each element of the tuple list.
Input:
[(12, 8), (5, 2), (1, 3, 7, 8)]
ele = 6
Output:
[(18, 14), (11, 8), (7, 9, 13, 14)]
We simply need to take each element of the list of tuples and add the given element to it. Multiple ways are used which we can perform the task, let explore them.
Method 1:
One method to solve the problem is by list comprehension to iterator the list of tuples and updating each element by adding the update element to it and then returning the updated list.
Output:
Method 2:
Another method to solve the problem is by using the map method to update the values using a lambda function that takes each element of tuple and added updateVal to it. All the updated map values are converted to a tuple using the tuple() method and encapsulated in a list.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer