Failed to import itertools in Python 3.5.2

I am new to Python. I am trying to import izip_longest from itertools. But I can not find the import of "itertools" in the settings in the Python interpreter. I am using Python 3.5.2. This gives me the error below -

from itertools import izip_longest ImportError: cannot import name 'izip_longest' 

Please let me know what the correct course of action is. I also tried Python 2.7 and ran into the same problem. I need to use a lower version of Python.

+6
source share
1 answer

izip_longest was renamed to zip_longest in Python 3 (note no i at the beginning), import instead:

 from itertools import zip_longest 

and use that name in your code.

If you need to write code that works in both Python 2 and 3, catch ImportError to try a different name and then rename:

 try: # Python 3 from itertools import zip_longest except ImportError: # Python 2 from itertools import izip_longest as zip_longest # use the name zip_longest 
+15
source

All Articles