importing izip from itertools module gives NameError in Python 3.x

I am trying to import the izip module like so:

from itertools import izip

However after recently changing over from Python 2.7 to 3 - it doesn't seem to work.

I am trying to write to a csv file:

writer.writerows(izip(variable1,2))

But I have no luck. Still encounter an error.

In Python 3 the built-in zip does the same job as itertools.izip in 2.X(returns an iterator instead of a list). The zip implementation is almost completely copy-pasted from the old izip, just with a few names changed and pickle support added.

Here is a benchmark between zip in Python 2 and 3 and izip in Python 2:

Python 2.7:

from timeit import timeit

print(timeit('list(izip(xrange(100), xrange(100)))',
             'from itertools import izip',
             number=500000))

print(timeit('zip(xrange(100), xrange(100))', number=500000))

Output:

1.9288790226
1.2828938961

Python 3:

from timeit import timeit

print(timeit('list(zip(range(100), range(100)))', number=500000))

Output:

1.7653984297066927

In this case since zip's arguments must support iteration you can not use 2 as its argument. So if you want to write 2 variable as a CSV row you can put them in a tuple or list:

writer.writerows((variable1,2))

Also from itertools you can import zip_longest as a more flexible function which you can use it on iterators with different size.

Itertools in Python 3, By Example – Real Python, I am trying to import the izip module like so: from itertools import izip. However after recently changing over from Python 2.7 to 3 - it doesn't  To return an iterator, the izip () and imap () functions of itertools must be used. In Python 3, izip () and imap () have been removed from itertools and replaced the zip () and map () built-ins. So, in a way, if you have ever used zip () or map () in Python 3, you have already been using itertools!

One of the ways which helped me is:

try:
    from itertools import izip as zip
except ImportError: # will be 3.x series
    pass

When to use izip (and how) in Python3 : learnpython, the thing is, whenever the iteration ends, the zip disappears. two lists that you could iterate whenever you wanted to, importing izip from itertools. (I'm always encouraging him to learn python and have done a little pygame with him). fast and gives me 20 new concepts every 20 minutes, if I decide to spend 2-3 hours  try: from future_builtins import zip except ImportError: # not 2.6+ or is 3.x try: from itertools import izip as zip # < 2.5 or 3.x except ImportError: pass The advantage of using future_builtin is that it's in effect a bit more "explicit" as to intended behaviour of the module, supported by the language syntax, and possibly recognised by tools.

Use zip instead of izip directly in python 3, no need to import anything.

For further visit here.

python3 doesn't need "from itertools import izip" · Issue #20 · nschloe , GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. import itertools Chaining multiple iterators together Use itertools.chain to create a single generator which will yield the values from several generators in sequence.

Import "izip" for different versions of Python, try: from itertools import izip as zip except ImportError: # will be 3.x series pass module, supported by the language syntax, and possibly recognised by tools. Questions on the subject: - questionscoding.com. By using this site, you consent to the use of cookies. You can refuse to use cookies by setting the necessary

Izip python 3, 26. reduce`` in Python 3+) - ``zip`` (returns ``itertools. 12. Import Zipfile class from zip file Python module. x instead because it's better if I learn Python 3 izip = zip try: # Python 2 xrange except NameError: # Python 3, Definition and Usage. This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to

Incompatibilities moving from Python 2 to Python 3, I am trying to import the izip module like so: from itertools import izip However after recently changing over from Python 2.7 to 3 - it doesn't seem to work. Q&A for speakers of other languages learning English. Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Comments
  • aargh - apparently I do not understand edit grace periods as well as I thought I did. now the edit logs are confusing
  • For further what? :)
  • For further understanding and looking at the patch where it was fixed. Maybe a good way to go. @bers