Python - Multiply all cross list element pairs


In the given problem statement we have to multiply all the cross list elements and create a new list of those products using the Python functionalities. In some circumstances, we must multiply each pair of items from the two lists to produce a single list containing the products. We will thus talk about the logic for solving this challenge in various ways.

Understanding the logic for the Problem

The problem at hand is to multiply the all cross elements of the given lists and create a single list which will be holding the products of these elements. To solve this problem we can use several approaches like iterating each item using nested loops and multiplying every item and storing it in the new list. Or we can use the lambda function by mapping the items of both the lists. And also we can solve this problem by using the numpy library of Python. So let’s see certain ways in which this task can be performed.

Approach 1 - Using nested loops

Algorithm

  • Step 1 − Define the function to perform the multiplication operation. Give name as multiply_cross_items and pass two parameters as sample_list1 and sample_list2.

  • Step 2 − Initialize the blank list as multiplication to hold the products of element pairs.

  • Step 3 − Using the nested loop we will iterate through the items of both the lists. Inside the second loop we will calculate the multiplication of i and j. And then append the product in the multiplication list.

  • Step 4 − Return the multiplication list and call the input lists and function to print the result.

Example

# Define the function to perform multiplication
def multiply_cross_items(sample_list1, sample_list2):
   multiplication = []
   # Nested for loops
   for i in sample_list1:
      for j in sample_list2:
         multiplication.append(i * j)
   return multiplication

# Call the lists and function
sample_list1 = [12, 14, 15]
sample_list2 = [10, 9, 8]
Output = multiply_cross_items(sample_list1, sample_list2)
print(Output)

Output

[120, 108, 96, 140, 126, 112, 150, 135, 120]

Approach 2 - Using lambda Function

Algorithm

  • Step 1 − Define the method to do the task and give the name as multiply_cross_items and pass two parameters as sample_list1 and sample_list2.

  • Step 2 − Using the map function we will iterate every item of both the lists and calculate the multiplication using the lambda function and store the result in the Output list.

  • Step 3 − Return Output list and call the input lists and function to print the result.

Example

# Function to multiply the given list
def multiply_cross_items(sample_list1, sample_list2):
   Output = list(
      map(lambda a: list(map(lambda b: a*b, sample_list2)), sample_list1))
   return [item for sub_list in Output for item in sub_list]

# Call the lists and function
list1 = [7, 8, 9]
list2 = [6, 5, 4]
Output = multiply_cross_items(list1, list2)
print(Output)

Output

[42, 35, 28, 48, 40, 32, 54, 45, 36]

Approach 3 - Using numpy

Algorithm

  • Step 1 − First import the library numpy using import keyword.

  • Step 2 − Define the method to perform the operation of multiplication and give the name of the function as multiply_cross_items and this function will accept two parameters as sample_list1 and sample_list2.

  • Step 3 − Inside the function, convert each list in the array using numpy. And give both the array name as array1 and array2.

  • Step 4 − Now we will use another array as multiply which will hold the product of items of the given lists. And using the outer function of numpy library we will calculate the product of the items of both the lists. And using the flatten function we will flat the array in a single array.

  • Step 5 − Now using the tolist method we will convert the above outcome in the list format.

  • Step 6 − Initialize the lists and call the function to see the Output.

Example

# Import the numpy library
import numpy as np
# Define the function to perform the task
def multiply_cross_items(sample_list1, sample_list2):
   array1 = np.array(sample_list1)
   array2 = np.array(sample_list2)
   multiply = np.outer(array1, array2).flatten()
   return multiply.tolist()

#Call the lists and function
sample_list1 = [4, 2, 6]
sample_list2 = [8, 5, 2]
Output = multiply_cross_items(sample_list1, sample_list2)
print(Output)

Output

[32, 20, 8, 16, 10, 4, 48, 30, 12]

Complexity

The time complexity for multiplying all cross list item pairs for all the codes is O(n²), as we are iterating over every item of both the lists to calculate the product of the elements. Here n is the number of items present in the lists.

Conclusion

So we have discussed how to multiply all cross list item pairs using Python. We have seen the logic and different algorithms to get the desired result with the time complexity of O(n²). All the codes allow us to calculate the products of cross list elements and create a new list with the Output.

Updated on: 17-Oct-2023

73 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements