JF_01
JF_01

Reputation: 21

how to create a loop for a generator function instead of each next call

I don`t understand how exactly should I use loop for to call all operations instead of using 'print (next(generator))

I have a generator function and I need to print all elements in the list

def gen(a,b)... #there is a generator function

for i in gen(a,b):
    print (next(gen(a,b)))

where is the problem?

Upvotes: 0

Views: 65

Answers (1)

Carcigenicate
Carcigenicate

Reputation: 45726

i is the value produced by your generator. With your current code, you're ignoring i, and creating a new generator in each iteration, then taking the first element of that new iterator.

You simply need:

for i in gen(a,b):
    print(i)

Upvotes: 1

Related Questions