# How to multiply the Elements of a Tuple in Python

Last updated: Apr 9, 2024
5 min

## #Multiply the elements of a tuple in Python

To multiply the elements of a tuple by a number:

1. Use a generator expression to iterate over the tuple.
2. Multiply each tuple element by a number.
3. Use the `tuple()` class to convert the result to a tuple.
main.py
```Copied!```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 first example uses a generator expression to multiply each element in a tuple by a certain number.

main.py
```Copied!```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.

## #Multiply the elements of a tuple using math.prod()

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

main.py
```Copied!```import math

my_tuple = (5, 5, 5)

result = math.prod(my_tuple)

print(result)  # ๐๏ธ 125
``````

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:

NameDescription
iterableAn iterable whose elements to calculate the product of
startThe 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.

## #Multiply the elements of a tuple using `reduce()`

This is a two-step process:

1. Pass a lambda function and the tuple to the `reduce()` function.
2. The lambda function should take the accumulator and the current value and should return the multiplication of the two.
main.py
```Copied!```from functools import reduce

my_tuple = (2, 3, 5)

result = reduce(lambda x, y: x * y, my_tuple)
print(result)  # ๐๏ธ 30
``````

The reduce() function takes the following 3 parameters:

NameDescription
functionA function that takes 2 parameters - the accumulated value and a value from the iterable.
iterableEach element in the iterable will get passed as an argument to the function.
initializerAn 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
```Copied!```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
```Copied!```from functools import reduce

my_tuple = (2,)

result = reduce(lambda acc, curr: acc * curr, my_tuple)
print(result)  # ๐๏ธ 2
``````

## #Multiply the elements of each Tuple in a List

To multiply the elements of each tuple in a list:

1. Use a list comprehension to iterate over the list.
2. Use the `math.prod()` function to calculate the product of the elements in each tuple.
main.py
```Copied!```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)
``````

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.

## #Multiply the elements of each Tuple in a List using a `for` loop

You can also manually multiply the elements of each tuple in a list in a `for` loop.

main.py
```Copied!```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
```Copied!```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]
``````

## #Multiply the elements of a tuple by a scalar using a `for` loop

You can also use a simple `for` loop if you need to multiply the elements of a tuple by a scalar.

main.py
```Copied!```my_tuple = (1, 2, 3)

results = []

for item in my_tuple:
results.append(item * 5)

print(results)  # ๐๏ธ [5, 10, 15]
``````

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.