Python Practice › List Programs › Program 34

Pair up two lists as sublists

EasyList Programs · 34 of 100

Write a Python program to combine items at the same position of two lists into sublists.

Advertisement
Input
[3, 5, 7, 8, 9], [1, 4, 3, 6, 2]
Output
[[3, 1], [5, 4], [7, 3], [8, 6], [9, 2]]

Approach

  1. zip pairs items; convert each pair to a list.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment