I have a list and I would like to get a new list such that each tuple in position 0 has 1 added to it. I can build a new list of tuples using loops but I want to understand how to do this with list comprehensions.
What I have:
in_list = [(0, 1), (1, 1), (2, 1), (3, 3), (4, 4)]
What I want:
in_list = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 4)]
>Solution :
Tuple is immutable type, so to increase the value at position 0, construct new tuple:
in_list = [(0, 1), (1, 1), (2, 1), (3, 3), (4, 4)]
in_list = [(a + 1, b) for a, b in in_list]
print(in_list)
Prints:
[(1, 1), (2, 1), (3, 1), (4, 3), (5, 4)]