Multiply all elements in a Tuple in Python

Borislav Hadzhiev

Last updated: Jul 9, 2022

Photo from Unsplash

**Use the math.prod() method to multiply all elements in a tuple, e.g.
result = math.prod(my_tuple). The math.prod() method calculates the product
of all the elements in the provided iterable.**

main.py

`import math from functools import reduce my_tuple = (2, 3, 5) # ✅ multiply all elements in tuple using math.prod() result = math.prod(my_tuple) print(result) # 👉️ 30 # ------------------------------- # ✅ multiply all elements in tuple using reduce() result_2 = reduce(lambda x, y: x * y, my_tuple) print(result_2) # 👉️ 30`

Make sure to import the `math`

module at the top.

We used the `math.prod`

method to multiply all the elements in a tuple.

The math.prod method calculates the product of all the elements in the provided iterable.

main.py

`import math my_tuple = (3, 3, 3) result = math.prod(my_tuple) print(result) # 👉️ 27`

The method takes the following 2 arguments:

Name | Description |
---|---|

iterable | An iterable whose elements to calculate the product of |

start | The start value for the product (defaults to `1` ) |

If the iterable is empty, the `start`

value is returned.

Alternatively, you can use the `reduce()`

function.

**To multiply all of the elements in a tuple:**

- Pass a lambda function and the tuple to the
`reduce()`

function. - The lambda function should take the accumulator and the current value, and should return the multiplication of the two.

main.py

`from functools import reduce my_tuple = (2, 3, 5) result = reduce(lambda acc, curr: acc * curr, my_tuple) print(result) # 👉️ 30`

The reduce function takes the following 3 parameters:

Name | Description |
---|---|

function | A function that takes 2 parameters - the accumulated value and a value from the iterable. |

iterable | Each element in the iterable will get passed as an argument to the function. |

initializer | An optional initializer value that is placed before the items of the iterable in the calculation. |

The lambda function gets called with the accumulated value and the value of the current iteration and multiplies them.

If we provide a value for the `initializer`

argument, it is placed before the
items of the iterable in the calculation.

main.py

`from functools import reduce my_tuple = (2, 3, 5) def do_math(acc, curr): print(acc) # 👉️ is 10 on first iteration return acc * curr result = reduce(do_math, my_tuple, 10) print(result) # 👉️ 300`

We passed `10`

for the initializer argument, so the value of the `accumulator`

will be `10`

on the first iteration.

The value of the

`accumulator`

would get set to the first element in the iterable if we didn't pass a value for the `initializer`

.If the `iterable`

is empty and the `initializer`

is provided, the `initializer`

is returned.

If the `initializer`

is not provided and the iterable contains only `1`

item,
the first item is returned.

main.py

`from functools import reduce my_tuple = (2,) result = reduce(lambda acc, curr: acc * curr, my_tuple) print(result) # 👉️ 2`