How to multiply the Elements of a Tuple in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
5 min

banner

# Table of Contents

  1. Multiply the elements of a tuple in Python
  2. Multiply the elements of a tuple using math.prod()
  3. Multiply the elements of a tuple using reduce()
  4. Multiply the elements of each Tuple in a List
  5. Multiply the elements of each Tuple in a List using a for loop
  6. Multiply the elements of a tuple by a scalar using a for loop

# 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
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)

multiply the elements of a tuple

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.

# 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
import math my_tuple = (5, 5, 5) result = math.prod(my_tuple) print(result) # ๐Ÿ‘‰๏ธ 125

multiply elements of tuple using math prod

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:

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
from functools import reduce my_tuple = (2, 3, 5) result = reduce(lambda x, y: x * y, my_tuple) print(result) # ๐Ÿ‘‰๏ธ 30

multiply elements of tuple using reduce

The code for this article is available on GitHub

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
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

# 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
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.

# 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
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]

# 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
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.

# Additional Resources

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

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev