Python product of two lists

Python Program to Find Product of List

Python Program to Find Product of List | Previously, we will add the list in python. In this program, we will find the product of a list. Like, the sum of lists we can perform multiplication too, In this section, we see some codes for python list multiplication by different methods.

  • How To Multiply Two Lists In Python
  • Python Multiply List By Scalar
  • Python Multiply List By Float
  • Multiply All Elements In List Python
  • Python Multiply All Elements In List By Constant
  • Multiply Each Element In List Python Numpy

How to Multipy Two List in Python

Now, we will see a simple program to multiply two lists with corresponding elements.

list1 = [5,6,4,3] list2 = [3,5,3,3] print("List1:", str(list1)) print("List2:", str(list2)) result = [] for i in range(0, len(list1)): result.append(list1[i] * list2[i]) print("Product:", str(result))

List1: [5, 6, 4, 3]List2: [3, 5, 3, 3]Product: [15, 30, 12, 9]

We have initialized two lists and initialized the third list that results in an empty list then use for loop over the range 0 to the length of the list and iterate it over all elements and multiply corresponding elements.

Читайте также:  Php move all files in directory

Python Program to Find Product of List by Sclar

Scalar multiplication is a multiplication of a vector by a scalar, it is a function from K X V to V where K is a field and V is a vector space over K. Syntax for scalar multiplication is as follows: [element * number of elements in list]

list = [4,6,7] result = [element * 2 for element in list] print(result) 

So, here we multiply each element in a list with 2 hence the result is as follows.

Python Program to Multiply List by Float

We can multiply float value to the list, let us see the example code for this.

list1 = [5,7,4,8] result = [] for i in range(0, len(list1)): result.append(list1[i] * 1.2) print(str(result))

The above code works as follows:-

Step1: First we initialize some values to the list
Step2: Then we print the list
Step3: Next we initialize the result to an empty list
Step4: Then in the for loop, we iterate over range 0 to the length of a list, and append the float multiplication to the result.
Step5: Print the result.

Multiply All Elements in List Python

Now, we multiply all the elements in a single list. This python program multiples all the ist elements and prints the output, unlike other programs we need only one list for this code.

list = [9,7,5] result = 1 for i in list: result = result * i print(result)

So, in the above code, we have initialized the list to value and then result to 1, usually we initialize the result to 0, as this is multiplication we use 1. Then in for loop, we iterate over the list to multiply each element and store it in the result. Finally, print the result.

Python Multiply All Elements in a List by Constant

This works similar to the scalar multiplication, here we can initialize constant to some value.

list = [4,3,3] a = 4 result = [b * a for b in list] print(result) 

We initialize the list, print the same then initialize a constant to some number, then in result, we multiply a list with constant, by iterating through for loop. Print the result.

Multiply Each Element in the List Python Numpy

Numpy is a built-in library in python, which has many functions, In this code, we use one such function.

import numpy as np l1 = [1,2,3] l2 = [3,4,5] print("List1:", l1) print("List2:", l2) result = np.multiply(l1, l2) print("Product:", result)

List1: [1, 2, 3]List2: [3, 4, 5]Product: [ 3 8 15]

In this code, we have imported NumPy as np and then initialized list1 and list2 the multiply two results using np.multiply() which takes two parameters list1 and list2, then print the result.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Источник

Cartesian Product of Two Lists in Python

In Python, we can get the Cartesian product of two lists easily. The easiest way to obtain the Cartesian product of two lists is with list comprehension.

list1 = ["a", "b", "c"] list2 = [1, 2, 3] cartesian_product = [(x,y) for x in list1 for y in list2] print(cartesian_product) #Output: [('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

You can also use a for loop to get the Cartesian product of two list objects in Python.

list1 = ["a", "b"] list2 = [1, 2] def cartesian_product(lst1, lst2): result = [] for x in lst1: for y in lst2: result.append((x,y)) return result print(cartesian_product(list1,list2)) #Output: [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Finally, the Python itertools module has a function product() which finds Cartesian products for you.

from itertools import product list1 = ["a", "b"] list2 = [1, 2] print(list(product(list1,list2))) #Output: [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

When working with collections of data in Python, the ability to manipulate them and create new collections is very valuable.

One such manipulation is the ability to get the Cartesian product of lists in a new list.

The Cartesian product of two sets A and B is the set of all possible ordered pairs (a, b), where a is in A and b is in B. We can get the Cartesian product between two lists easily with Python.

The easiest way to get the Cartesian product of two lists is with list comprehension.

Below is a simple example of how to find the Cartesian product of two lists in Python using list comprehension.

list1 = ["a", "b", "c"] list2 = [1, 2, 3] cartesian_product = [(x,y) for x in list1 for y in list2] print(cartesian_product) #Output: [('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)]

Using for Loop to Get Cartesian Product of Lists in Python

We can also use a loop to get the Cartesian product of lists in Python.

By definition, the Cartesian product of two collections of elements are all possible ordered pairs.

We can define a loop easily which will loop over all possible combinations of our lists and create ordered pairs in the form of tuples.

Below is a simple example of how to find the Cartesian product of two lists in Python using iteration.

list1 = ["a", "b"] list2 = [1, 2] def cartesian_product(lst1, lst2): result = [] for x in lst1: for y in lst2: result.append((x,y)) return result print(cartesian_product(list1,list2)) #Output: [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Using itertools product() Function to Get Cartesian Product of Lists in Python

The itertools module has many great functions which allow us to iterate over collections and perform complex tasks easily.

We can use the itertools product() function to calculate the Cartesian product of lists.

To get the Cartesian product of multiple lists in Python using product(), just pass the lists to the function.

Below is a simple example of how to find the Cartesian product of two lists in Python using itertools and product().

from itertools import product list1 = ["a", "b"] list2 = [1, 2] print(list(product(list1,list2))) #Output: [('a', 1), ('a', 2), ('b', 1), ('b', 2)]

Hopefully this article has been useful for you to learn how to get the cartesian product of lists in Python.

  • 1. How to Convert pandas Column dtype from Object to Category
  • 2. Using Python to Get Home Directory
  • 3. How to Check if Character is Uppercase in Python
  • 4. How to Clear Turtle Screen in Python with clear() Function
  • 5. pandas mad – Calculate Mean Absolute Deviation in Python
  • 6. Read First Line of File Using Python
  • 7. How to Measure Execution Time of Program in Python
  • 8. Find All Pythagorean Triples in a Range using Python
  • 9. Using Python to Split String by Newline
  • 10. Negate Boolean in Python with not Operator

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Источник

Оцените статью