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?
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With