I wrote a very simple generator function:
def prefixes(xs):
prefix = []
for x in xs:
prefix.append(x)
yield prefix
I expected the output to be:
>>> list(prefixes([1, 2, 3]))
[[1], [1, 2], [1, 2, 3]]
But I got:
>>> list(prefixes([1, 2, 3]))
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
That is very weird. How does the first iteration yield all the list? current
should only contain the first item. Why is the output what it is and not what I expect?
P.S. Searching for "python list.append in generators" in Google did not give relevant results.