0

I ran the following code:

import itertools
my_list = ['a', 'b', 'c', 'd', 'e']
for i in itertools.izip([x for x in my_list], [y for y in itertools.count()]):
        print i

Based on the documentation for izip, I expected it to return a series of tuples, ending when my_list ran out: (a,0) (b,1) (c,2) (d,3) (e,4)

Instead, I get the memory error copied below. It seems the itertools.count() function is returning infinite data, rather than being limited to the 5 records in my_list?

line 50, in main
    for i in itertools.izip([x for x in range(10)], [y for y in itertools.count()]): MemoryError

2 Answers 2

4

Yes, itertools.count was designed to never stop counting. That is why it returns an iterator, which produces items on demand rather than all at once.

However, this part of your code:

[y for y in itertools.count()]

effectively tells Python to place the infinite amount of numbers produced by itertools.count into a single list. Thus, a MemoryError is raised because this operation immediately consumes all available memory.


You can fix your problem by simply removing the list comprehension1:

>>> import itertools
>>> my_list = ['a', 'b', 'c', 'd', 'e']
>>> for i in itertools.izip(my_list, itertools.count()):
...     print i
...
('a', 0)
('b', 1)
('c', 2)
('d', 3)
('e', 4)
>>>

With this change, itertools.izip will iterate over the iterator returned by itertools.count(), which will now only produce as many numbers as are needed.


1I removed your other list comprehension too because it is unnecessary: my_list is already iterable because it is a list.

Sign up to request clarification or add additional context in comments.

Comments

0

Why not just this?

for a,b in enumerate(my_list):
    print (b, a)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.