How to make zip_longest available in itertools using Python 2.7

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

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])))