import random
my_list = [random.randint(3, 100) for i in range(4)]
def is_even(some_list):
for i in some_list:
if i % 2 == 0:
yield True
else:
yield False
print(my_list)
print(list(is_even(my_list)))
>>> [94, 53, 27, 42]
>>> [True, False, False, True]
It seems that I still do not fully understand the concept. On each iteration, the is_even
function yields True
or False
accordingly. I do not see how those “yields” are accumulated. (Or, I do not see how they are appended to the final list at the end of each iteration. For example, in the given example, the first iteration yields True
, then the second iteration starts. Where is that first True
value kept?) What exactly is happening there?