Multiply the elements of a tuple in Python

avatar

Borislav Hadzhiev

Last updated: Jul 2, 2022

banner

Photo from Unsplash

Multiply the elements of a tuple in Python #

To multiply the elements of a tuple:

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

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

main.py
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.

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)

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.

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.