Python – Cross Pattern Pairs in List

PythonServer Side ProgrammingProgramming

When it is required to display cross pattern pairs in list, a list comprehension and the ‘*’ operator are used.

Below is a demonstration of the same −

Example

my_list_1 = [14, 35, 26]
my_list_2 = [36, 24, 12]

print("The first list is :")
print(my_list_1)
print("The second list is :")
print(my_list_2)

result = [i * j for j in my_list_1 for i in my_list_2]

print ("The result is :")
print(result)

Output

The first list is :
[14, 35, 26]
The second list is :
[36, 24, 12]
The result is :
[504, 336, 168, 1260, 840, 420, 936, 624, 312]

Explanation

• Two lists are defined and displayed on the console.

• A list comprehension is used to iterate over the list, and product of the two lists is calculated.

• This result is assigned to a variable.

• This is the output that is displayed on the console.

Published on 08-Sep-2021 08:46:05