Two For Loops in List Comprehension - Python
Last Updated :
03 Dec, 2024
List comprehension is a concise and readable way to create lists in Python. Using two for loops in list comprehension is a great way to handle nested iterations and generate complex lists.
Using two for-loops
This is the simplest and most efficient way to write two for loops in a list comprehension. Let's suppose we want to combine two loops inside one. We have two lists, and we want to combine their values in pairs.
Python
x = [1, 2]
y = [3, 4]
# Using two for loops inside a list comprehension
ans = [(a, b) for a in x for b in y]
print(ans)
Output[(1, 3), (1, 4), (2, 3), (2, 4)]
There are various methods to write two loops in List Comprehension in Python.
Filter Based on a Condition (Adding an if Clause)
We can also add a condition to filter the results while using two for loops. Let’s say we only want pairs where the sum of a and b is greater than 4.
Python
x = [1, 2, 3]
y = [3, 4, 5]
# Using two for loops and an if condition to filter
ans = [(a, b) for a in x for b in y if a + b > 4]
print(ans)
Output[(1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
Nested Loops with Different Range
Sometimes, we might want to generate numbers or perform a calculation based on the ranges. In such cases, using two for loops is helpful.
Python
# Generating pairs of numbers from 1 to 3 and 4 to 6
ans = [(a, b) for a in range(1, 4) for b in range(4, 7)]
print(ans)
Output[(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]
Using Two Loops with a More Complex Condition
Now let’s try something a bit more complex. We want to generate pairs where a is even and b is odd.
Python
# Generating pairs with specific conditions
ans = [(a, b) for a in range(1, 6) for b in range(1, 6) if a % 2 == 0 and b % 2 != 0]
print(ans)
Output[(2, 1), (2, 3), (2, 5), (4, 1), (4, 3), (4, 5)]
Similar Reads
Python List Comprehension With Two Lists List comprehension allows us to generate new lists based on existing ones. Sometimes, we need to work with two lists together, performing operations or combining elements from both. Let's explore a few methods to use list comprehension with two lists.Using zip() with List ComprehensionOne of the mos
3 min read
4 Tips To Master Python List Comprehensions Python's list comprehensions offer a concise and powerful way to create lists. They allow you to express complex operations in a single line of code, making your code more readable and efficient. In this article, we will explore four tips to master Python list comprehensions with five commonly used
4 min read
How to Create List of Dictionary in Python Using For Loop The task of creating a list of dictionaries in Python using a for loop involves iterating over a sequence of values and constructing a dictionary in each iteration. By using a for loop, we can assign values to keys dynamically and append the dictionaries to a list. For example, with a list of keys a
3 min read
How to Input a List in Python using For Loop Using a for loop to take list input is a simple and common method. It allows users to enter multiple values one by one, storing them in a list. This approach is flexible and works well when the number of inputs is known in advance.Letâs start with a basic way to input a list using a for loop in Pyth
2 min read
How to Check End of For Loop in Python In Python, we often use loops to iterate over a collection like list or string. But sometimes, we may want to know when the loop has reached its end. There are different ways to check for the end of a for loop. Let's go through some simple methods to do this.Using else with For Loop (Most Efficient)
2 min read
Add Elements of Two Lists in Python Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read