[Tutor] update a list

Manprit Singh manpritsinghece at gmail.com
Sun Oct 11 04:44:21 EDT 2020


Dear sir ,

Consider a problem where I have to update a list , at even places with
increment of 1 and at odd places with increment of 2 . Just need to check
if this can be done in a better way. My solution is given below:

l = [2, 3, 5, 7, 6, 4]
for i, j in enumerate(l):
    if i % 2 == 0:
        l[i] = j+1
    else:
        l[i] = j+2

the updated list is

[3, 5, 6, 9, 7, 6]

Regards
Manprit Singh


More information about the Tutor mailing list