How to multiply the Elements of a Tuple in Python

Borislav Hadzhiev

Last updated: Apr 9, 2024

Reading timeยท5 min

- Multiply the elements of a tuple in Python
- Multiply the elements of a tuple using math.prod()
- Multiply the elements of a tuple using reduce()
- Multiply the elements of each Tuple in a List
- Multiply the elements of each Tuple in a List using a for loop
- Multiply the elements of a tuple by a scalar using a for loop

To multiply the elements of a tuple by a number:

- Use a generator expression to iterate over the tuple.
- Multiply each tuple element by a number.
- Use the
`tuple()`

class to convert the result to a tuple.

main.py

`import math # โ Multiply the elements of a tuple by a scalar my_tuple = (5, 3) by_five = tuple(5 * elem for elem in my_tuple) print(by_five) # ๐๏ธ (25, 15) # ----------------------------------------------- # โ Multiply the elements of a tuple my_tuple = (5, 5, 5) result = math.prod(my_tuple) print(result) # ๐๏ธ 125 # ----------------------------------------------- # โ Multiply the elements of each tuple in a list list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = [math.prod(tup) for tup in list_of_tuples] # ๐๏ธ [2, 12, 30] print(result)`

The code for this article is available on GitHub

The first example uses a generator expression to multiply each element in a tuple by a certain number.

main.py

`my_tuple = (5, 3) by_five = tuple(5 * elem for elem in my_tuple) print(by_five) # ๐๏ธ (25, 15)`

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we multiply the current tuple element by `5`

and return the
result.

The last step is to use the tuple()
class to convert the `generator`

object to a tuple.

If you simply need to multiply the numbers in a tuple, use the `math.prod()`

method.

If you need to multiply all elements in a tuple, use the `math.prod()`

method.

main.py

`import math my_tuple = (5, 5, 5) result = math.prod(my_tuple) print(result) # ๐๏ธ 125`

The code for this article is available on GitHub

Instead of multiplying each element in the tuple by a specific number, this example multiplies the elements in the tuple.

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

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.

An alternative way to multiply the elements of a tuple is to use the `reduce()`

function.

`reduce()`

This is a two-step process:

- 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 x, y: x * y, my_tuple) print(result) # ๐๏ธ 30`

The code for this article is available on GitHub

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`

To multiply the elements of each tuple in a list:

- Use a list comprehension to iterate over the list.
- Use the
`math.prod()`

function to calculate the product of the elements in each tuple.

main.py

`import math list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = [math.prod(tup) for tup in list_of_tuples] # ๐๏ธ [2, 12, 30] print(result)`

The code for this article is available on GitHub

This example multiplies the values in each tuple in a list.

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we use the `math.prod()`

function to calculate the product of
the tuple's elements.

The final list contains the multiplication results for each tuple.

`for`

loopYou can also manually multiply the elements of each tuple in a list in a `for`

loop.

main.py

`list_of_tuples = [(1, 2), (3, 4), (5, 6)] result = [] for tup in list_of_tuples: result.append(tup[0] * tup[1]) print(result) # ๐๏ธ [2, 12, 30]`

Instead of using the `math.prod()`

method, we access the elements of each tuple
and multiply them manually.

If your tuples have more than 2 elements, you'd have to tweak the expression to include the next tuple elements.

main.py

`list_of_tuples = [(1, 2, 1), (3, 4, 3), (5, 6, 5)] result = [] for tup in list_of_tuples: result.append(tup[0] * tup[1] * tup[2]) print(result) # ๐๏ธ [2, 36, 150]`

`for`

loopYou can also use a simple `for`

loop if you need to multiply the elements of a
tuple by a scalar.

main.py

`my_tuple = (1, 2, 3) results = [] for item in my_tuple: results.append(item * 5) print(results) # ๐๏ธ [5, 10, 15]`

The code for this article is available on GitHub

We declared a new variable and initialized it to an empty list.

On each iteration, we multiply the current tuple element by `5`

and append the
result to the new list.

Want to learn more about multiplying values in Python? Check out these resources: Multiply the Values in a Dictionary in Python,Multiply each element in a List by a Number in Python.

You can learn more about the related topics by checking out the following tutorials: