How to sum only even numbers in a list?
Note: Trust yourself. Sometimes, what you think is wrong is actually right.
Given a situation where you just want to sum even numbers in a list, the following code would be useful to add even numbers to a new list, called total
, and then print the sum
.
items = [23, 555, 666, 123, 128, 4242, 990]
total = []
for each_item in items:
if each_item % 2 == 0:
total.append(each_item)
print(sum(total))