Python | Iterate over multiple lists simultaneously (original) (raw)
Last Updated : 08 May, 2025
In Python, iterating over a single list is straightforward using a for loop. But if we want to iterate over multiple lists at the same time-accessing corresponding elements from each in one loop then we can do so by using these methods:
- zip()
- itertools.zip_longest()
- enumerate()
- Generator expressions inside zip()
Using zip()
The built-in zip() function combines elements from multiple iterables. It stops when the shortest iterable is exhausted.
Python `
num = [1, 2, 3] colors = ['red', 'white', 'black'] values = [255, 256]
for n, c, v in zip(num, colors, values): print(n, c, v)
`
Output
1 red 255 2 white 256
**Explanation:
- **zip() pairs the first element from each list, then the second, and so on.
- Since values has only **2 elements, the loop runs only **twice.
If we want the loop to continue until the longest list is exhausted, use can use **itertools.zip_longest().
Python `
import itertools
num = [1, 2, 3] col = ['red', 'white', 'black'] val = [255, 256]
for n, c, v in itertools.zip_longest(num, col, val): print(n, c, v)
`
Output
1 red 255 2 white 256 3 black None
**Explanation: When a list runs out of items, None is used by default for the missing values.
We can also specify a default value instead of **None in **zip_longest() using the **fillvalue paramaeter.
Python `
import itertools
num = [1, 2, 3] colors = ['red', 'white', 'black'] values = [255, 256]
for n, c, v in itertools.zip_longest(num, colors, values, fillvalue=-1): print(n, c, v)
`
Output
1 red 255 2 white 256 3 black -1
**Note: In Python 2.x, zip() returned a list, and itertools.izip() returned an iterator. In Python 3.x, zip() already returns an iterator, so izip() and izip_longest() are no longer needed.
Using enumerate() with Indexing
If you want to track the index while accessing multiple lists, enumerate() is handy:
Python `
a = [1, 2, 3] b = ['a', 'b', 'c'] c = ['x', 'y', 'z']
for i, val in enumerate(a): print(val, b[i], c[i])
`
Explanation:
- enumerate() gives both the index (i) and the element (val) of “a**“.
- We use the index to fetch matching elements from “b” and **“c”.
Also read: zip(), enumerae(), zip_longest().
Similar Reads
- Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s 6 min read
- Get a list as input from user in Python We often encounter a situation when we need to take a number/string as input from the user. In this article, we will see how to take a list as input from the user using Python. Get list as input Using split() MethodThe input() function can be combined with split() to accept multiple elements in a si 3 min read
- Create List of Numbers with Given Range - Python The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8 3 min read
- Python - Add List Items Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples: Add a Single Item Using append()append() method adds one item to the end of the l 3 min read
- How to add Elements to a List in Python In Python, lists are dynamic which means that they allow further adding elements unlike many other languages. In this article, we are going to explore different methods to add elements in a list. For example, let's add an element in the list using append() method: [GFGTABS] Python a = [1, 2, 3] a.ap 2 min read