I tried to implement the sieve of Eratosthenes with iterators (because I wanted to get more into functional programming with python). Sadly some unexpected behaviour occurred. You can see it here in this video: https://i.stack.imgur.com/woEu5.jpg
Here is my code:
def sieve_primes(stop=10):
L = (x for x in range(2, stop+1))
while True:
prime = next(L)
L = filter(lambda x: x % prime != 0 or x == prime, L)
#L, M = itertools.tee(L)
#print(list(M))
yield prime
It works (spits out an iterator object with the desired primes) when the two commented lines are uncommented. Otherwise, it just iterates over every number.
I'm looking forward to your answers :) Thanks!