Python Practice › List Programs › Program 48

Iterate two lists at once

EasyList Programs · 48 of 100

Write a Python program to iterate over two lists simultaneously and build a list of pairs.

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

Approach

  1. zip both lists.

Solution

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

More list programs

← All 100 List Programs

Leave a Comment