Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling none in maps in Python 3 [duplicate]

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?

like image 338
Echchama Nayak Avatar asked Jan 25 '16 21:01

Echchama Nayak


1 Answers

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

like image 149
Martijn Pieters Avatar answered Nov 07 '22 03:11

Martijn Pieters