How to loop through two lists in parallel till the longest list ends with Python

2 Answers

0 votes
import itertools

list1 = [1, 2, 3, 4, 5, 10, 20]
list2 = [7, 8, 9, 0, 6]

for a, b in itertools.zip_longest(list1, list2):
    print(a, b)






'''
run:

1 7
2 8
3 9
4 0
5 6
10 None
20 None

'''

 



answered Apr 20, 2021 by avibootz
0 votes
import itertools

list1 = [1, 2, 3, 4, 5]
list2 = [7, 8, 9, 12, 6, 10, 20, 30]

for a, b in itertools.zip_longest(list1, list2, fillvalue = 0):
    print(a, b)






'''
run:

1 7
2 8
3 9
4 12
5 6
0 10
0 20
0 30

'''

 



answered Apr 20, 2021 by avibootz

Related questions

1 answer 199 views
1 answer 179 views
1 answer 152 views
4 answers 289 views
289 views asked Jan 10, 2021 by avibootz
1 answer 306 views
1 answer 177 views
...