Last updated: Apr 9, 2024
Reading timeยท5 min
To multiply the elements of a tuple by a number:
tuple()
class to convert the result to a tuple.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.
my_tuple = (5, 3) by_five = tuple(5 * elem for elem in my_tuple) print(by_five) # ๐๏ธ (25, 15)
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.
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:
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:
reduce()
function.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:
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. |
If we provide a value for the initializer
argument, it is placed before the
items of the iterable in the calculation.
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.
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.
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:
math.prod()
function to calculate the product of the elements in
each tuple.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.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: