Python Practice › List Programs › Program 48
Iterate two lists at once
Write a Python program to iterate over two lists simultaneously and build a list of pairs.
Input
[1, 3, 5, 7, 9], [8, 6, 4, 2, 10]
Output
[[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
Approach
zipboth lists.
Solution
a = [1, 3, 5, 7, 9]b = [8, 6, 4, 2, 10]print([[x, y] for x, y in zip(a, b)])Output
[[1, 8], [3, 6], [5, 4], [7, 2], [9, 10]]
Learn the concept
New to this topic? Read the lesson, then come back.