python

Iterate over two Lists simultaneously in Python

Sometimes, you need to iterate over two lists at the same time. For example, let’s say that you want to iterate over two lists, and for every element in the first list, you want to do something else.

In this article, we will learn how to iterate over two lists at the same time in Python. If you are new to Python, check this beginner’s guide to learn some essential tips to get started.

Let’s begin.

What Is Iterate Over Two Lists Simultaneously in Python?

This article will show you how to iterate over two lists at the same time in Python. We will learn multiple methods to iterate over two lists in Python.

Iterate over two lists simultaneously can be useful if you have to iterate over two lists quickly, or if you have to print the list elements one by one. For example, let’s say that you want to keep track of all the rated items in a shopping cart. In such a case, you can keep a list of items, and while checking out the items, you can also rate them.

How to Iterate Over Two Lists Simultaneously in Python?

We will use Python For loop and zip() function here to loop through two lists simultaneously.

Code Example

first_names = ["Rick", "John", "Tony"]
last_names = ["Grimes", "Wick", "Stark"]

fullname_list = []

for firstname, lastname in zip(first_names, last_names):
  full_name = firstname + ' ' + lastname
  fullname_list.append(full_name)

print(fullname_list)

Output

['Rick Grimes', 'John Wick', 'Tony Stark']

Iterate Over Two Lists Simultaneously using List comprehension and zip() function

We can iterate over two lists at the same time using the "list comprehension" and zip() function in Python. The code example is as follows:

first_names = ["Rick", "John", "Tony"]
last_names = ["Grimes", "Wick", "Stark"]

fullname_list = [x + ' ' + y for x, y in zip(first_names, last_names)]

print(fullname_list)

Output

['Rick Grimes', 'John Wick', 'Tony Stark']

List comprehension

A list comprehension is a concise way to define a set of list operations. Let’s say that we want to print the sum of all the numbers from two lists - A and B. Then we can use the following code for that.

A = [1, 2, 3, 4, 5, 6]
B = [10, 20, 30, 40, 50, 60]

sum = [a + b for a, b in zip(A, B)]

print(sum)

Output

[11, 22, 33, 44, 55, 66]

This is possible because list comprehension allows us to write concise code.

Iterate over more than two lists Simultaneously

We can also use the zip() function and List comprehension to iterate over more than two lists. You can understand it using the below code example.

list1 = [1, 1, 1, 1, 1]
list2 = [2, 2, 2, 2, 2]
list3 = [3, 3, 3, 3, 3]

list_sum = [x+y+z for x,y,z in zip(list1, list2, list3)]

print(list_sum)

Output

[6, 6, 6, 6, 6]

Conclusion

Iterate over two lists simultaneously in Python can be useful if you want to quickly print the list elements, or if you want to do something else with every list element. We can iterate over two lists using the “list comprehension”, zip() function, and For Loop in Python.

The above methods will help you iterate over two lists at the same time in Python.

Was this helpful?