Reuse of the same list object. Your generator returns one object over and over, manipulating it as it arrives, but any other references to it see the same changes:
>>> g = fun(); y = [x for x in g]; y [[2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1]] >>> y[0] is y[1] True >>> y[0][0] = 42 >>> y [[42, 1], [42, 1], [42, 1], [42, 1], [42, 1], [42, 1], [42, 1], [42, 1], [42, 1], [42, 1]]
Assume a copy of the list, or create a new new list object instead of processing it.
def fun(): state = [1, 2] for i in range(10): for j, var in enumerate(state): next_st = np.random.randint(0, 3) state[j] = next_st yield state[:] # copy def fun(): for i in range(10): state = [1, 2] # new list object each iteration for j, var in enumerate(state): next_st = np.random.randint(0, 3) state[j] = next_st yield state