creating multiple generators inside a list comprehension
generator, list-comprehension, python
Solution
The problem is that the name `v` in your generator expression refers to that variable `v` in the list comprehension. So, when the code your generator expression actually runs (when you call `next`), it looks at the variable `v` and sees the value `12`, no matter what the value of `v` was when you created the generator.
One workaround:
deck = range(52)
def select_kth(v):
return (i for i in deck if i % 13 == v)
gens = [select_kth(v) for v in range(13)]
Because we defined a function, the name `v` gets to live in its own naming environment and so stays around unmodified.
If you really wanted, you could do this in one line:
gens = [(lambda v: (i for i in deck if i % 13 == v))(v) for v in range(13)]
Problem
I am trying to group cards of the same suit (color) and rank inside generators and store those generators inside a list comprehension. The solution I came up with does that except for the fact that all the generators contain exactly the same cards. Any idea why? Here is the code ``` deck=range(52) gens=[(i for i in deck if i%13==v) for v in range(13)] ``` Based on this I would expect for example: ``` gens[1].next() 1 gens[1].next() 14 gens[10].next() 10 gens[10].next() 23 ``` But instead I get ``` gens[1].next() 12 gens[1].next() 25 gens[1].next() 38 ``` And all the generators in the list return the same results..