Write a Python program to replace last value of tuples in a list


The program is written in Python and it modifies a list of tuples by adding an element to each tuple. Initially, a list l is created containing 3 tuples, each with 3 elements. The program uses a list comprehension to modify each tuple in the list l. The list comprehension creates a new list by iterating over the tuples in l and using the slicing operator [:-1] to exclude the last element of each tuple. The resulting slice is then concatenated with a tuple (10,) containing a single element 10. Finally, the program prints the resulting list.

Source Code

l = [(5, 2, 3), (4, 7, 6), (8, 9, 6)]
print([t[:-1] + (10,) for t in l])
 

Output

[(5, 2, 10),(4, 7, 10),(8, 9, 10)]

Example Programs