VOOZH about

URL: https://www.geeksforgeeks.org/python/iterate-over-two-lists-with-different-lengths-in-python/

⇱ Iterate over Two Lists with Different Lengths in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Iterate over Two Lists with Different Lengths in Python

Last Updated : 23 Jul, 2025

Iterating over two lists of different lengths is a common scenario in Python. However, we may face challenges when one list is shorter than the other. Fortunately, Python offers several methods to handle such cases, ensuring that our code is both readable and efficient.

Using zip() function - Loop until shortest list ends

zip() function is one of the simplest ways to iterate over two or more lists. It pairs elements by their positions but stops when the shorter list ends.

Using itertools.zip_longest()

The itertools module has itertools.zip_longest() function, which is ideal for iterating over lists of different lengths. This function fills the shorter list with a specified value (default is None) to ensure that both lists can be traversed completely.


Output
1 a
2 b
3 c
None d
None e

Here is the list of all methods to Iterate over Two lists with different lengths in Python.

Using for Loop with range() and len()

For full control over how you iterate over two lists of different lengths, you can use a for loop.


Output
1 a
2 b
3 c
None d
None e

Using enumerate() - Loop until shortest list ends

enumerate() function can be combined with manual checks for lengths to provide more control over iteration when working with two lists of different lengths.


Output
1 a
2 b
3 c
Comment
Article Tags: