• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Iterate Over Two Lists

Using Python to Iterate Over Two Lists

August 29, 2022 Leave a Comment

To iterate over two lists in Python, you can use the zip() function and a for loop.

lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]

for x, y in zip(lst_1, lst_2):
    print(x, y)

#Output:
0 a
1 b
2 c

If you have lists which don’t have the same length, you can use the itertools module zip_longest() function.

import itertools

lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c", "d"]

for x, y in itertools.zip_longest(lst_1, lst_2):
    print(x, y)

#Output:
0 a
1 b
2 c
None d

When working with collections of data, the ability to iterate over those collections in an easy way is valuable.

One such case is if you have two lists and want to iterate over both lists at the same time in Python.

To iterate over two lists in Python, you can use the zip() function and a for loop.

The Python zip() function zips lists together and returns a zip object, which is an iterator of tuples where each item from each list is paired together.

If the passed iterable objects have different lengths, the iterator with the least items decides the length of the new iterator.

You can then unpack the returned zip object and use the returned values in a for loop.

Below shows how you can iterate over two lists using zip() and a loop in Python.

lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]

for x, y in zip(lst_1, lst_2):
    print(x, y)

#Output:
0 a
1 b
2 c

Using itertools zip_longest() Function to Iterate Over Lists of Different Length

If you want to zip two lists which don’t have the same length, you should be aware that the zip() function will remove values from the longer list.

For example, if you have a list of three and five items and try to zip them together, you will get a zip object with three items.

list1 = ["apple", "banana", "cherry"]
list2 = [1, 2, 3, 4, 5]

print(list(zip(list1, list2)))

#Output:
[('apple', 1), ('banana', 2), ('cherry', 3)]

If you want to zip based on the list with the longer length, you can use the itertools module zip_longest() function.

zip_longest() zips two lists and for the items where we cannot pair values, because we don’t have a value from the shorter list, zip_longest() fills in None.

With zip_longest(), you can do the same as above and zip two lists together and iterate over them.

Below shows you how to use zip_longest() to iterate over two lists of different lengths in Python.

import itertools

lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c", "d"]

for x, y in itertools.zip_longest(lst_1, lst_2):
    print(x, y)

#Output:
0 a
1 b
2 c
None d

How to Iterate Over More Than Two Lists in Python

If you have more than two lists, then you can use the same structure as above and just add the additional lists to zip().

You can use the same code structure as above and unpack what is returned from zip() as shown below to iterate over more than two lists in Python.

lst_1 = [0, 1, 2]
lst_2 = ["a", "b", "c"]
lst_3 = [4, 5, 6]
lst_4 = ["d","e","f"]

for x, y, z, a in zip(lst_1, lst_2, lst_3, lst_4):
    print(x, y, z, a)

#Output:
0 a 4 d
1 b 5 e
2 c 6 f

Hopefully this article has been useful for you to learn how to iterate over two lists in Python.

Other Articles You'll Also Like:

  • 1.  Remove Substring from String in Python with replace()
  • 2.  Using Python to Print Variable Type
  • 3.  pandas cumsum – Find Cumulative Sum of Series or DataFrame
  • 4.  islower Python – Check if All Letters in String Are Lowercase
  • 5.  Using Python to Convert Tuple to String
  • 6.  How to Multiply Two Numbers in Python
  • 7.  Get Quarter from Date in pandas DataFrame
  • 8.  Examples of Recursion in Python
  • 9.  How to Group By Columns and Find Minimum in pandas DataFrame
  • 10.  Find Quotient and Remainder After Division in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy