Python Practice › List Programs › Program 34
Pair up two lists as sublists
Write a Python program to combine items at the same position of two lists into sublists.
Input
[3, 5, 7, 8, 9], [1, 4, 3, 6, 2]
Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
Approach
zippairs items; convert each pair to a list.
Solution
a = [3, 5, 7, 8, 9]b = [1, 4, 3, 6, 2]print([[x, y] for x, y in zip(a, b)])Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]
Learn the concept
New to this topic? Read the lesson, then come back.