5

I am doing the following in Python2.7:

>>> a = [1,2,3,4,5]
>>> b = [2,1,3,4]
>>> c = [3,4]
>>> map(None, a, b, c)
[(1, 2, 3), (2, 1, 4), (3, 3, None), (4, 4, None), (5, None, None)]

I am trying to do something similar in Python3

>>> a = [1,2,3,4,5]
>>> b = [2,1,3,4]
>>> c = [3,4]
>>> map(None, a, b, c)
<map object at 0xb72289ec>
>>> for i,j,k in map(None, a, b, c):
...  print (i,j,k)
... 
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

How do I replicate the Python2 results in Python3?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Echchama Nayak
  • 971
  • 3
  • 23
  • 44
  • 1
    I think [this](http://stackoverflow.com/questions/12015521/python-3-vs-python-2-map-behavior) is what you are looking for – SirParselot Jan 25 '16 at 21:25

1 Answers1

8

Use the itertools.zip_longest() function instead:

from itertools import zip_longest

for i, j, k in zip_longest(a, b, c):

This zips together the three lists, padding out with the fillvalue keyword value (defaulting to None).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343