How to multiply two lists in Python?


In the given problem statement we have to create an algorithm for multiplying two lists using Python. There can be many situations in which we can use this approach like Data Preprocessing, Combining Information, Scaling values and much more.So we will demonstrate different methods and algorithms to do this kind of work.

Understanding the Problem

The problem at hand is to calculate the multiplication of two given lists using Python. So basically we will be given two common lists of Python and we have to multiply items of the first list with the items of the second list. For example let us see the below image.

Logic for The Above Problem

To solve this problem of multiplying two given lists we need to traverse over the respective items of both the lists and then multiply them with each other. So the result will be a new list of the products of the items of both the lists. There are also some other methods with the help of it we can multiply two lists: using for loop, using list comprehension and using numpy library.

Approach 1: Using a for loop

In this approach we will be using a for loop to iterate through the items of both the lists and multiply them with the corresponding items. And store the multiplied values in the separate list called multiplied_list. The time complexity using for loop is O(n), here n is the number of items present in the lists.

Example

#multiply using for loop
# Initialize the two lists
first_list = [2, 4, 4, 6, 1]
second_list = [8, 2, 5, 7, 3]

# initialize result list
multiplied_list = []
for i1, i2 in zip(first_list, second_list):
   multiplied_list.append(i1*i2)

print("The multiplication of two lists: ", multiplied_list)

Output

The multiplication of two lists:  [16, 8, 20, 42, 3]

Approach 2: Using a list Comprehension

In this approach we will use a list comprehension. So list comprehension is the way with the help of it we can create a new list as per the given list and the given condition. In this approach we will also use the zip function which is an iterative method to iterate through the items of the lists

The time complexity for multiplying two lists using list comprehension is O(n), here n is the size of the given lists. As we have multiplied the items of the list and used the zip function which takes n times to complete the task.

Example

#multiply using list comprehension
# Initialize the two lists
first_list = [12, 14, 14, 16, 21]
second_list = [18, 12, 15, 17, 13]
multiplied_list = [a1 * a2 for a1, a2 in zip(first_list, second_list)]

print("The multiplied list is: ", multiplied_list)

Output

The multiplied list is:  [216, 168, 210, 272, 273]

Approach 3: Using numpy

In this approach we will use the numpy library of Python. Whenever we need to work on arrays we can use the numpy library. Numpy library contains some mathematical functions to work on arrays. So we will use the multiply function of the numpy library and multiply each corresponding item of the given lists. The time complexity for multiplying two lists using numpy is O(n), because the size of the first and second lists is n.

Example

#multiply using numpy library

# import the numpy library
import numpy as nmp

# Initialize the two lists
first_list = [11, 12, 13, 14, 15]
second_list = [1, 2, 3, 4, 5]

#multiply two lists using numpy
multiplied_list = nmp.multiply(first_list, second_list)

# printing multiplied list
print("The multiplied list is : " + str(multiplied_list))

Output

The multiplied list is : [11 24 39 56 75]

Conclusion

So this is the way we can multiply two lists item wise by iterating over the corresponding items of both the lists and multiplying them to get the result. We have solved the given problem in different ways and all the approaches are efficient in terms of time complexity.

Updated on: 16-Oct-2023

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements