Open In App

Python List Comprehension With Two Lists

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We are given two lists and our task is to show the implementation of list comprehension with those two lists. In this article, we will see how we can use list comprehension with two list in Python and perform various operations on them.

Python List Comprehension With Two Lists

Below are some of the ways by which we can use list comprehension with multiple lists in Python:

Basic List Comprehension

In this example, the code adds corresponding elements from two lists, `list_a` and `list_b`, using Python list comprehension with the `zip` function. The result is a new list containing the sums. When printed, it outputs `[6, 8, 10, 12]`.

Python3




list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
 
result = [a + b for a, b in zip(list_a, list_b)]
print(result)


Output

[6, 8, 10, 12]

Using Conditional Statement

In this example, code creates a new list, `result`, by adding corresponding elements from `list_a` and `list_b` using list comprehension with a condition. It filters out elements whose sum is not even. The printed result is a list containing only the even sums, which is `[6, 10]`.

Python3




list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
 
result = [a + b for a, b in zip(list_a, list_b) if (a + b) % 2 == 0]
print(result)


Output

[6, 8, 10, 12]

Using Nested Loops

In this example, below code uses nested loops as list comprehension to create a new list, `result`, by multiplying each element from `list_a` with every element from `list_b`. The output is a flattened list of all possible products, resulting in `[5, 6, 7, 8, 10, 12, 15, 18, 21, 24, 30, 32]`.

Python3




list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
 
result = [a * b for a in list_a for b in list_b]
print(result)


Output

[5, 6, 7, 8, 10, 12, 14, 16, 15, 18, 21, 24, 20, 24, 28, 32]

Using Custom Function

In this example, code defines a custom function, `custom_function(a, b)`, which calculates the sum of squares of its arguments. Then, it uses list comprehension to create a new list, `result`, by applying this custom function to corresponding elements from `list_a` and `list_b`.

Python3




list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
 
def custom_function(a, b):
    return a**2 + b**2
 
result = [custom_function(a, b) for a, b in zip(list_a, list_b)]
print(result)


Output

[26, 40, 58, 80]

Using Enumerate() Function

In this example, below code utilizes list comprehension with `enumerate` to create a new list, `result`, where each element is the product of the index and the corresponding pair of elements from `list_a` and `list_b`. The output is `[0, 6, 14, 24]`, representing the index multiplied by the sum of each pair.

Python3




list_a = [1, 2, 3, 4]
list_b = [5, 6, 7, 8]
 
result = [(index+1) * value for index, value in enumerate(zip(list_a, list_b))]
print(result)


Output

[(1, 5), (2, 6, 2, 6), (3, 7, 3, 7, 3, 7), (4, 8, 4, 8, 4, 8, 4, 8)]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads