Multiple iterators (using enumerate) for the same iterable, what is going on?

Nils :

Consider the following example:

s = 'abc'
[(c1, c2) for j, c2 in enumerate(s) for i, c1 in enumerate(s)]

Output:

[('a', 'a'),
 ('b', 'a'),
 ('c', 'a'),
 ('a', 'b'),
 ('b', 'b'),
 ('c', 'b'),
 ('a', 'c'),
 ('b', 'c'),
 ('c', 'c')]

I would expected the same output if enumerate is called outside the list comprehension and the iterators are assigned to variables:

it1, it2 = enumerate(s), enumerate(s)
[(c1, c2) for j, c2 in it1 for i, c1 in it2]

But I get:

[('a', 'a'), ('b', 'a'), ('c', 'a')]

What is going on? I use Python 3.6.9.

norok2 :

What is happening is that the inner iterator gets exhausted after the first iteration of the outer iterator:

s = 'abc'
it1 = enumerate(s)
it2 = enumerate(s)

for i, x in it1:
    for j, y in it2:  # <-- gets consumed when i = 0 and stays empty
        ...

By contrast:

s = 'abc'

for i, x in enumerate(s):
    for j, y in enumerate(s):  # <-- gets recreated at each iteration
        ....

If you need persistence, enclose it in a list or tuple:

itr = list(enumerate(s))
print([(c1, c2) for j, c2 in itr for i, c1 in itr])
# [('a', 'a'), ('b', 'a'), ('c', 'a'), ('a', 'b'), ('b', 'b'), ('c', 'b'), ('a', 'c'), ('b', 'c'), ('c', 'c')]

although note the different memory footprint of using enumerate() multiple times versus having it enclosed in a list or tuple.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360110&siteId=1