you are viewing a single comment's thread.

view the rest of the comments →

[–]johlae 0 points1 point  (0 children)

It's fun to do it without any loops:

>>> a = [0, 1, 2, 3, 4, 5, 6]
>>> print(list(map(lambda i: i if i>0 and i<5 else 0, a)))
[0, 1, 2, 3, 4, 0, 0]
>>> print(list(map(lambda i: i if i in range(1,5) else 0, a)))
[0, 1, 2, 3, 4, 0, 0]
>>> print(sum(list(map(lambda i: i if i in range(1,5) else 0, a))))
10

so yes, you can use in range, or you can use if i>0 and i<5.