Iterate over Two Lists with Different Lengths in Python Last Updated : 22 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 endszip() 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. Python a = [1, 2, 3] b = ['a', 'b', 'c', 'd', 'e'] # zip() pairs elements from each list by their index positions for item1, item2 in zip(a, b): print(item1, item2) Table of ContentUsing itertools.zip_longest()Using for Loop with range() and len()Using enumerate() - Loop until shortest list endsUsing 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. Python from itertools import zip_longest a = [1, 2, 3] b = ['a', 'b', 'c', 'd', 'e'] # Iterates over both lists, filling missing values with None for item1, item2 in zip_longest(a, b, fillvalue=None): print(item1, item2) # comprehensive code for above loop # result = [(a, b) for a, b in zip_longest(x, y, fillvalue=None)] Output1 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. Python l1 = [1, 2, 3] l2 = ['a', 'b', 'c', 'd', 'e'] # Determine the maximum length between the two lists max_len = max(len(l1), len(l2)) # Iterate over a range from 0 to max_length - 1 for i in range(max_len): # Get the element from x at index i if it exists, otherwise use None item1 = l1[i] if i < len(l1) else None # Get the element from y at index i if it exists, otherwise use None item2 = l2[i] if i < len(l2) else None print(item1, item2) Output1 a 2 b 3 c None d None e Using enumerate() - Loop until shortest list endsenumerate() function can be combined with manual checks for lengths to provide more control over iteration when working with two lists of different lengths. Python l1 = [1, 2, 3] l2 = ['a', 'b', 'c', 'd', 'e'] # Iterate over the length of x (the shorter list) using enumerate for i, _ in enumerate(l1): # Check if within bounds of y, but this is a logical error a = l1[i] if i < len(l2) else None # Get element from y at index i b = l2[i] print(a, b) Output1 a 2 b 3 c Comment More infoAdvertise with us Next Article Iterate over Two Lists with Different Lengths in Python P punam6fne Follow Improve Article Tags : Python Python function-programs Practice Tags : python Similar Reads Iterate over a list in Python Python provides several ways to iterate over list. The simplest and the most common way to iterate over a list is to use a for loop. This method allows us to access each element in the list directly.Example: Print all elements in the list one by one using for loop.Pythona = [1, 3, 5, 7, 9] # On each 3 min read Ways to Iterate Tuple List of Lists - Python In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi 3 min read How to Find Length of a list in Python The length of a list means the number of elements it contains. In-Built len() function can be used to find the length of an object by passing the object within the parentheses. Here is the Python example to find the length of a list using len().Pythona1 = [10, 50, 30, 40] n = len(a1) print("Size of 2 min read Python | Intersection of two lists The task of finding the intersection of two lists involves identifying the common elements between them. This means we want to extract the values that appear in both lists, while ignoring any duplicates or values that are unique to each list. For example, if we have two lists [4, 9, 1, 17, 11] and [ 4 min read Python | Extract Combination Mapping in two lists Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method 2 min read Like