How to multiply two lists in Python

This guide delves into multiple techniques for multiplying two lists in Python. We’ll cover both straightforward and efficient methods for performing element-wise multiplication between lists. This proves especially valuable when working with two lists of integers or floating-point numbers that share the same length, as it allows us to multiply corresponding elements. By the end of this guide, you’ll have a solid grasp of different strategies to accomplish this task and generate a resulting list that matches the input lists in size.

If you haven’t already, we highly recommend exploring our other Python tutorials on Lists. These resources provide additional insights and examples that can further enrich your comprehension of these concepts.

Method 1: 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))'
 
Output:
Product of both lists is : [4, 15, 24, 12, 80]

In this code, we have two lists, list1 and 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 list1 and list2 using the * operator. The result is then appended to the result list.

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.

Method 2: 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, list1 and 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)
Output:
[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.

Method 3: 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)
Output:
The product of both lists is: [9, 16, 21, 24, 25]

You can also use the multiply() function of the numpy library to multiply the elements of two lists.

Method 4: Multiplying two lists using the map() function

The 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, x and y represent the corresponding elements from list1 and list2. The function returns the product of these two elements. The map() function applies the lambda function to each pair of corresponding elements from list1 and list2, resulting in a map object. To get the final result as a list, we convert the map object into a list using the list() function.

By using the map() function, we can perform element-wise multiplication of two lists in a concise and efficient manner.

Method 5: Handling Unequal Lengths with Zip Function

In scenarios where one list has more elements than the other, the zip() function becomes instrumental in aligning the elements for multiplication. The following example demonstrates this technique:

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)

This approach ensures that each element is multiplied with the corresponding element from the other list, accommodating varying list lengths.

Conclusion

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.

Leave a Comment

Your email address will not be published. Required fields are marked *