How to combine two unequal-length lists element‑by‑element using zip_longest in Python

2 Answers

0 votes
from itertools import zip_longest

# two unequal-length lists
nums1 = [1, 2, 3, 4] 
nums2 = [10, 20, 30]

combine = list(zip_longest(nums1, nums2)) 

print(combine)



'''
run:

[(1, 10), (2, 20), (3, 30), (4, None)]

'''

 



answered Jan 19 by avibootz
0 votes
from itertools import zip_longest

keys = ['name', 'age', 'country', 'profession']
data = [['Lanna'], ['34'], ['California', 'Texas']]

combine = dict(zip_longest(keys, data))

print(combine)


'''
run:

{'name': ['Lanna'], 'age': ['34'], 'country': ['California', 'Texas'], 'profession': None}

'''

 



answered Jan 19 by avibootz
...