Mar-06-2017, 09:30 PM
If you really wanted to use a while loop, you could just check against the list itself (an empty list is the same as False), and .pop() instead of re-building the list each iteration. So...
items = [2, 4, 6, 8] total = 0 while items: total += items.pop() print(total)Or, if you're a map/reduce/functional fan (and don't want to just use sum() for some reason), then there's yet another way:
from functools import reduce import operator items = [2, 4, 6, 8] print(reduce(operator.add, items))