In this tutorial, we will explore various methods for multiplying two lists in Python. We will discuss simple and efficient techniques to perform element-wise multiplication of two lists. This is particularly useful when dealing with two lists of integers or floating-point numbers of the same size, where we want to multiply corresponding elements. By the end of this tutorial, you will gain a comprehensive understanding of different approaches to achieve this and generate a resulting list of the same size as the input lists.
Here are several common methods for multiplying two lists:
- Using a for loop
- Using List Comprehension
- Utilizing the Numpy library
- Employing the map() function
If you haven’t already, we highly recommend checking out our other tutorials on Lists in Python. These tutorials offer additional insights and examples that can further enhance your understanding of these concepts.
Multiply Two Lists using for loop
The simplest and most straightforward technique to multiply two lists is by using a for loop. This method involves iterating through the lists using a for loop and multiplying the elements at the corresponding indices. Here’s an example that demonstrates how to multiply two lists using a for loop:
list1 = [1, 3, 4, 6, 8] list2 = [4, 5, 6, 2, 10] result =  for x in range(0, len(list1)): result.append(list1[x] * list2[x]) print ("Product of both lists is : " + str(result))'
Product of both lists is : [4, 15, 24, 12, 80]
In this code, we have two lists,
list2, which contain numbers. We initialize an empty list called
result to store the multiplied values.
Using a for loop, we iterate through the range from 0 to the length of
list1. For each iteration, we multiply the elements at the corresponding indices in
list2 using the
* operator. The result is then appended to the
Finally, we use the
print() function to display the resulting list, converted to a string using the
str() function. The output will show the product of the corresponding elements from both lists.
Another interesting situation arises when list1 has more elements than list2. In such cases, the
zip() function can be used to ensure that each element is multiplied with the corresponding element from the other list, discarding the last elements if necessary.
zip() function takes two or more iterables and returns an iterator that generates tuples containing elements from each iterable at corresponding positions. Within each iteration of the for loop, we multiply the elements from
list2 using the variables
num2. The multiplied value is then appended to the
result list. Below is the code that demonstrates the same example using the
list1 = [1, 2, 3, 4, 5, 6] list2 = [6, 5, 4, 3, 3] result =  for i1, i2 in zip(list1, list2): result.append(i1 * i2) print("The product of the two lists is:", result)
The product of the two lists is: [6, 10, 12, 12, 15]
By using a for loop with either the range or zip function, it is possible to multiply corresponding elements from two lists. This approach allows for easy multiplication and storage of the results in a new list.
Multiply two lists using List comprehension
List comprehension is a simple and concise method for multiplying two lists in Python. It offers an easy-to-understand syntax and can be used to achieve the desired result without any complexity. If you are unfamiliar with list comprehension, you can visit this resource for more information.
The code below uses list comprehension to multiply the corresponding elements of two lists,
list2. The result is stored in the
result list and then printed.
#Using list comprehension list1 = [9, 8, 7] list2 = [1, 3, 5] result = [i1 * i2 for i1, i2 in zip(list1, list2)] print(result)
[9, 24, 35]
Since we are using the
zip() function in this code, we don’t need to worry about the length of the lists. The
zip() function will pair up the elements from both lists and multiply them together until it reaches the last element of the shortest list.
Multiplying two lists using numpy arrays
To begin, we need to import the NumPy library. Next, we determine the list with the fewest elements. We convert both lists into NumPy arrays. Finally, we perform element-wise multiplication of list1 and list2 using their corresponding index numbers.
#Using Numpy import numpy as x list1 = [1, 2, 3, 4, 5] list2 = [9, 8, 7, 6, 5, 6] n = min(len(list1), len(list2)) array1 = x.array(list1) array2 = x.array(list2) result = array1[:n]*array2[:n] result = result.tolist() print("The product of both lists is: ", result)
The product of both lists is: [9, 16, 21, 24, 25]
You can also use the multiply() function of numpy library to multiply the elements of two lists.
Multiplying two lists using map() function
map() function in Python is a built-in function that applies a given function to each item of an iterable (e.g., a list, tuple, or set) and returns an iterator that yields the results. When it comes to multiplying two lists using the
map() function, we can apply a lambda function or a predefined function to each pair of corresponding elements from the two lists. Here is how it works in detail:
list1 = [1, 4, 7.3, 2.7, 8] list2 = [9, 6.5, 2, 5.27, 3] result = list(map(lambda x, y: x * y, list1, list2)) print("Result: ", result)
Result: [9, 26.0, 14.6, 14.229, 24]
In the lambda function,
y represent the corresponding elements from
list2. The function returns the product of these two elements. The
map() function applies the lambda function to each pair of corresponding elements from
list2, resulting in a map object. To get the final result as a list, we convert the map object into a list using the
By using the
map() function, we can perform element-wise multiplication of two lists in a concise and efficient manner.
To conclude, there are multiple ways to multiply two lists in Python. The most straightforward approach is to use a for loop and iterate through the lists, multiplying the elements at the corresponding indices. Another method is to use the zip() function to combine the elements from both lists and perform multiplication. List comprehension provides a concise way to achieve the same result. Additionally, the map() function can be used to apply a multiplication function to each element of the lists. Choose the method that suits your requirements and coding style. Remember to consider factors such as the length of the lists and the need for handling different scenarios, such as when the lists have unequal lengths.
If you have any questions, please leave feedback here. Make sure to see our other tutorials to learn Python.
See more Python List Tutorials