How To Multiply Each Element In A Tuple By A Number In Python

Multiply each element in a tuple by a number in Python

Multiply each element in a tuple by a number in Python is an interesting topic. I will show you some ways you can do it, like using list comprehension, converting to a numpy array, multiplying two tuples using the zip function, and using math.prod() function. Please read the following article.

Multiply each element in a tuple by a number in Python

Use the list comprehension

Using list comprehension is one way you can multiply the elements of a tuple in Python.

Example:

  • The tuple elements are traversed by the list comprehension and multiplied by 10. Finally, a tuple is returned by the tuple() function.
myTuple = (10, 10)

# Use the list comprehension to multiply the elements of a tuple
result = tuple([z * 10 for z in myTuple])

print('New tuple:', result)

Output:

New tuple: (100, 100)

Use the np.array function

Syntax:

np.array(value1, value2, ….)

Parameters:

  • value: value to be formatted.

Example:

  • Create a tuple.
  • Convert tuple to numpy array.
  • Multiply the elements of an array by a number.
  • Finally, the tuple() function converts the numpy values ​​to tuples.
import numpy as np

myTuple = (10, 10)

# The Tuple object is converted to a numpy array
result = tuple(10*np.array(myTuple))

print('New tuple:', result)

Output:

New tuple: (100, 100)

Multiply the values ​​of two tuples together

You can multiply each element of one tuple by the element of another using the zip() function.

Syntax:

zip(*iterable)

Parameter:

  • iterable: built-in iterables (like list, string, dict) or user-declared iterable.

The zip() function returns a zip object that is a list iterator, tuples combined with other elements from the iterator.

Example:

  • Initialize two tuples.
  • The two tuples are unpacked using the zip function and iterated by a list comprehension.
  • Every element from the first tuple is a multiple of the corresponding element in the second tuple.
  • Finally, the tuple() function converts it to a tuple.
myTuple1 = (1, 2, 3)
myTuple2 = (4, 5, 6)

# Use the zip function
result = tuple(elem_1 * elem_2 for elem_1, elem_2 in zip(myTuple1, myTuple2))

print('New tuple:', result)

Output:

New tuple: (4, 10, 18)

Use the math.prod() function

Since Python version 3.8, the math.prod() function is included in the math module. You can use this function to calculate the product of the elements of an iterable object.

Syntax:

math.prod(iterable, start)

Parameters:

  • iterable: Objects such as LIST, TUPLE, or SET need to produce the elements.
  • start: Marks start to calculate elements. The parameter is not required.

The math.prod() function will return the product of the elements of the iterable object.

Example:

  • Import the math module.
  • Multiply the elements of the tuple together using math.prod() function.
import math

myTuple = (10, 10)

# Multiply the elements of the tuple using math.prod function
result = math.prod(myTuple)

print('Multiplication result:', result)

Output:

Multiplication result: 100

Summary

I want to share how you can multiply the elements of a tuple in Python, and I suggest you use the math.prod function to do this. If you like these topics, visit our website to read more. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *