Python product of two lists

In this post, we will see how to multiply two lists in Python. We want to find the product of list elements at corresponding positions.

Simply put, we have to perform the element-wise multiplication. So, lets get started.

Multiply two Lists in Python by Using a Loop

One straightforward approach is to iterate over both lists, compute the product of values at the same index and append it to a new list. Lets see an example.

list_one=[3,4,5,6,8,9] list_two=[4,5,1,0,6] product=[] forel1,el2inzip[list_one,list_two]: product.append[el1*el2] print["Theproductoftwolistsis:",product]

Output

The product of two lists is: [12, 20, 5, 0, 48]

In the above example, we iterate over lists using the zip[] method. It takes iterables [lists] and returns an iterator of tuples.

Moreover, make sure to run a loop n times, where n is the length of the smaller list.

The zip[] method takes care of that as you can notice that list_one has six items and list_two has five, but the product list has five values only.

Multiply two Lists in Python by Using List Comprehension

We can also use the list comprehension to perform element-wise multiplication. This method should be preferred over the previous one because it is easier and concise. Lets see.

list_one=[3,4,5,6,8,9] list_two=[4,5,1,0,6] product=[el1*el2forel1,el2inzip[list_one,list_two]]#element-wisemultiplication print["Theproductoftwolistsis:",product]

Output

The product of two lists is: [12, 20, 5, 0, 48]

See, this is a short and simple approach.

Multiply two Lists in Python by Using NumPy

Another method is to use the NumPy library. First, we convert both lists to NumPy arrays or ndarrays, i.e., arr1 and arr2.

Then, we multiply two arrays like we do numbers, i.e., arr1*arr2. Finally, we convert the ndarray to a list.

However, the length of both lists needs to be the same. Otherwise, we will get an error. Lets see.

importnumpyasnp list_one=[3,4,5,6,8,9] list_two=[4,5,1,0,6,] n=min[len[list_one],len[list_two]]#gettheminimumlength #converttonumpyarrays arr1=np.array[list_one] arr2=np.array[list_two] product=arr1[:n]*arr2[:n]#element-wisemultiplication #convertnumpyarraytolist product=product.tolist[] print["Theproductoftwolistsis:",product]

Output

The product of two lists is: [12, 20, 5, 0, 48]

Here, we take the length of the smaller array and multiply only that number of elements.

NumPy is a powerful library for scientific computations. It provides several methods and tools to create and work with arrays efficiently.

Therefore, if we want to calculate the element-wise product of large data, then using NumPy will be very efficient.

Hey guys! Its me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Dont let anyone tell you that this cant be done. Skys the limit, reallyas long as you BELIEVE in it! And it all starts right here..at Maschituts!

Video liên quan

Chủ Đề