# Python – Multiply two list

There can be many situations in which one requires to find index wise product of two different lists. This can have a possible applications in day-day programming. Lets discuss various ways in which this task can be performed.

Method #1 : Naive Method In this method, we simply run a loop and append to the new list the product of the both list elements at similar index till we reach end of the smaller list. This is the basic method to achieve this task.

## Python3

 `# Python code to demonstrate ``# Multiplying two lists``# naive method ` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# printing original lists``print` `("Original ``list` `1` `: " ``+` `str``(test_list1))``print` `("Original ``list` `2` `: " ``+` `str``(test_list2))` `# using naive method to ``# Multiplying two lists``res_list ``=` `[]``for` `i ``in` `range``(``0``, ``len``(test_list1)):``    ``res_list.append(test_list1[i] ``*` `test_list2[i])` `# printing resultant list ``print` `("Resultant ``list` `is` `: " ``+` `str``(res_list))`

Output :
```Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [4, 15, 24, 12, 80]```

Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.

Method #2 : Using List Comprehension The shorthand for the above explained technique, list comprehensions are usually quicker to type and hence must be preferred to perform these kind of programming tasks.

## Python3

 `# Python code to demonstrate ``# Multiplying two lists``# list comprehension` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# printing original lists``print` `("Original ``list` `1` `: " ``+` `str``(test_list1))``print` `("Original ``list` `2` `: " ``+` `str``(test_list2))` `# using list comprehension to ``# Multiplying two lists``res_list ``=` `[test_list1[i] ``*` `test_list2[i] ``for` `i ``in` `range``(``len``(test_list1))]` `# printing resultant list ``print` `("Resultant ``list` `is` `: " ``+` `str``(res_list))`

Output :
```Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [4, 15, 24, 12, 80]```

Method#3: Using Recursive method.

## Python3

 `def` `multiply_lists_recursive(list1, list2, index``=``0``):``    ``if` `index ``=``=` `len``(list1):``        ``return` `[]``    ``return` `[list1[index] ``*` `list2[index]] ``+` `multiply_lists_recursive(list1, list2, index ``+` `1``)` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# printing original lists``print``(``"Original list 1:"``, test_list1)``print``(``"Original list 2:"``, test_list2)` `# using the recursive function to multiply two lists``result ``=` `multiply_lists_recursive(test_list1, test_list2)` `# printing result``print``(``"Resultant list is:"``, result)``#this code contributed by tvsk`

Output
```Original list 1: [1, 3, 4, 6, 8]
Original list 2: [4, 5, 6, 2, 10]
Resultant list is: [4, 15, 24, 12, 80]```

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 : Using operator.mul()

Approach

1. Initiated a for loop

2. Used operator.mul() for multiplying the list elements index wise

3. Appended the product to output list

4. Displayed output list

## Python3

 `# Python code to demonstrate``# Multiplying two lists``# naive method` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# printing original lists``print` `(``"Original list 1 : "` `+` `str``(test_list1))``print` `(``"Original list 2 : "` `+` `str``(test_list2))` `# using naive method to``# Multiplying two lists``res_list ``=` `[]``import` `operator``for` `i ``in` `range``(``0``, ``len``(test_list1)):``    ``res_list.append(operator.mul(test_list1[i],test_list2[i]))` `# printing resultant list``print` `(``"Resultant list is : "` `+` `str``(res_list))`

Output
```Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [4, 15, 24, 12, 80]```

Time Complexity : O(N)
Auxiliary Space : O(N)

Method #5 : Using the map() function

Algorithm:

1. Initialize two lists to be multiplied.
2. Use map and lambda function to multiply the two lists element-wise.
3. Convert the resulting map object to a list.
4. Print the resultant list.

## Python3

 `# Initializing two lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# Using map and lambda function to multiply two lists element-wise``res_list ``=` `list``(``map``(``lambda` `x, y: x ``*` `y, test_list1, test_list2))` `# Printing the resultant list``print``(``"Resultant list is : "` `+` `str``(res_list))`

Output
`Resultant list is : [4, 15, 24, 12, 80]`

Time Complexity: O(n)
The map function applies the lambda function to each pair of elements in the two lists, which takes O(1) time. Therefore, the time complexity of the map function is O(n). The time complexity of converting the resulting map object to a list is also O(n). Thus, the overall time complexity of this approach is O(n).

Auxiliary Space: O(n)
The resultant list requires O(n) space to store the multiplied elements. Therefore, the auxiliary space complexity of this approach is also O(n).

Justification:
This approach uses the map and lambda function to multiply the two lists element-wise. It is a simple and concise approach that avoids the use of for loops, which can be time-consuming. The map function can be faster than using a for loop because it applies the lambda function to each pair of elements in the two lists simultaneously. Thus, the time complexity of this approach is O(n), which is optimal, and the auxiliary space complexity is O(n), which is also optimal.

Method#6:Using numpy

Algorithm:

1. Import the numpy library.
2. Initialize two lists test_list1 and test_list2.
3. Print the original lists.
4. Use numpy.multiply() method to multiply the two lists element-wise and store the result in res_list.
5. Print the final result.

## Python3

 `# importing numpy library``import` `numpy as np` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# printing original lists``print``(``"Original list 1 : "` `+` `str``(test_list1))``print``(``"Original list 2 : "` `+` `str``(test_list2))` `# using numpy library to multiply two lists``res_list ``=` `np.multiply(test_list1, test_list2)` `# printing resultant list``print``(``"Resultant list is : "` `+` `str``(res_list))``#This code is cintributed by Vinay Pinjala.`

Output

```Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [4, 15, 24, 12, 80]```

Time Complexity: O(n), where n is the length of the larger list. This is because the numpy library is optimized for fast mathematical operations on arrays and is much faster than the naive method used in the original code.
Auxiliary Space: O(n), where n is the length of the larger list. This is because the numpy library creates a new array to store the result of the multiplication operation.

Method #7: Using NumPy’s element-wise multiplication function, np.multiply()

Use NumPy’s element-wise multiplication function, np.multiply(), to perform the same operation. It first converts the lists to NumPy arrays, uses np.multiply() to perform element-wise multiplication, and then converts the resulting NumPy array back to a list.

step-by-step approach of the program:

1. The first line imports the NumPy library as np. NumPy is a popular library used for scientific computing in Python, and it provides support for working with arrays and matrices.
2. The next two lines initialize two lists called test_list1 and test_list2. These lists contain some numerical values.
3. The following two lines use the NumPy function np.array() to convert the lists test_list1 and test_list2 to NumPy arrays called arr1 and arr2, respectively. NumPy arrays are more efficient for mathematical operations than regular Python lists.
4. The fourth line uses the NumPy function np.multiply() to perform element-wise multiplication of the two NumPy arrays arr1 and arr2. The result is stored in a new NumPy array called res_arr.
5. The next line uses the NumPy function tolist() to convert the NumPy array res_arr back to a regular Python list called res_list.
6. Finally, the last line prints the resultant list by using the print() function.

## Python3

 `import` `numpy as np` `# initializing lists``test_list1 ``=` `[``1``, ``3``, ``4``, ``6``, ``8``]``test_list2 ``=` `[``4``, ``5``, ``6``, ``2``, ``10``]` `# converting lists to numpy arrays``arr1 ``=` `np.array(test_list1)``arr2 ``=` `np.array(test_list2)` `# using np.multiply() to element-wise multiply two arrays``res_arr ``=` `np.multiply(arr1, arr2)` `# converting the resultant numpy array back to a list``res_list ``=` `res_arr.tolist()` `# printing the resultant list``print``(``"Resultant list is: "``, res_list)`

Output:

`Resultant list is:  [4, 15, 24, 12, 80]`

Time complexity: O(n), where n is the length of the lists.
Auxiliary space: O(n), since we’re creating two numpy arrays of size n each, and then converting the resulting numpy array back to a list of size n.

Previous
Next