13

When trying to import this function on a Python Jupyter 2.7 nb running on Windows 10, I get this error:

enter image description here

I believe I hadn't encountered problems in the past because I was using Python 3. So I wonder if it is just that it is not available in Python 2, or if there is a way of making it work.

Antoni Parellada
  • 4,253
  • 6
  • 49
  • 114

2 Answers2

34

For Python 3, the method is zip_longest:

from itertools import zip_longest

For Python 2, the method is izip_longest:

from itertools import izip_longest
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
9

If you don't know which version of python runs the script you can use this trick:

try:
    from itertools import zip_longest
except ImportError:
    from itertools import izip_longest as zip_longest

# now this works in both python 2 and 3
print(list(zip_longest([1,2,3],[4,5])))
Alireza Afzal Aghaei
  • 1,184
  • 10
  • 27