Friday, December 27, 2024
Google search engine
HomeLanguagesPython – How to Multiply all items in Tuple

Python – How to Multiply all items in Tuple

Sometimes, while programming, we have a problem in which we might need to perform product among tuple elements. This is an essential utility as we come across product operation many times and tuples are immutable and hence required to be dealt with. Let’s discuss certain ways in which this task can be performed. 

Method #1 : Using list() + loop The above functions can be combined to perform this task. We can employ loop to accumulate the result of product logic. The list() function is used to perform interconversions. 

Python3




# Python3 code to demonstrate working of
# Tuple Elements Multiplication
# Using list() + loop
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing tup
test_tup = (7, 8, 9, 1, 10, 7)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Tuple Elements Multiplication
# Using list() + loop
res = prod(list(test_tup))
 
# printing result
print("The product of tuple elements are : " + str(res))


Output : 

The original tuple is : (7, 8, 9, 1, 10, 7)
The product of tuple elements are : 35280

Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(n), to store the list created from the tuple.

Method #2 : Using map() + loop + list() The combination of above functions can be used to perform this task. In this, we first convert the tuple to list, flatten it’s each list element using map(), perform product of each using loop and again employ loop for overall product of resultant list. 

Python3




# Python 3 code to demonstrate working of
# Tuple Elements Multiplication
# Using map() + list() + loop
 
# getting Product
def prod(val) :
    res = 1
    for ele in val:
        res *= ele
    return res 
 
# initializing tup
test_tup = ([7, 8], [9, 1], [10, 7])
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Tuple Elements Multiplication
# Using map() + list() + loop
res = prod(list(map(prod, list(test_tup))))
 
# printing result
print("The product of tuple elements are : " + str(res))


Output : 

The original tuple is : (7, 8, 9, 1, 10, 7)
The product of tuple elements are : 35280

Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required

Method #3 : Here is an approach using the reduce function from the functools library, along with an anonymous lambda function:

Python3




# Python 3 code to demonstrate working of
# Tuple Elements Multiplication
# Using reduce() and lambda function
   
from functools import reduce
   
# getting Product
def prod(val) :
    return reduce((lambda x, y: x * y), val)
   
# initializing tup
test_tup = (7, 8, 9, 1, 10, 7)
   
# printing original tuple
print("The original tuple is : " + str(test_tup))
   
# Tuple Elements Multiplication
# Using reduce() and lambda function
res = prod(test_tup)
   
# printing result
print("The product of tuple elements are : " + str(res))


Output

The original tuple is : (7, 8, 9, 1, 10, 7)
The product of tuple elements are : 35280

Time Complexity: O(n), where n is the length of the tuple, as it needs to iterate through the tuple once to calculate the product. 
Auxiliary Space: O(1), as it only uses a single variable res to store the result.

Method #4 : Using functools.reduce() and operator.mul

Python3




# Python3 code to demonstrate working of
# Tuple Elements Multiplication
 
# initializing tup
test_tup = (7, 8, 9, 1, 10, 7)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Tuple Elements Multiplication
from functools import reduce
import operator
res=reduce(operator.mul, test_tup , 1)
 
# printing result
print("The product of tuple elements are : " + str(res))


Output

The original tuple is : (7, 8, 9, 1, 10, 7)
The product of tuple elements are : 35280

Time Complexity : O(N)
Auxiliary Space : O(1)

Method 5: Using numpy module

Note: use “pip install numpy” to install

we use the np.prod() function of the numpy module to find the product of all the elements in the input tuple. The np.prod() function takes an array or a sequence of numbers as input and returns the product of all the elements in it.

  • Import the numpy module using the import statement.
  • Initialize a tuple test_tup with the given set of values .
  • Print the original tuple using the print() function and concatenation operator.
  • Use the np.prod() function of the numpy module to find the product of all the elements in the input tuple.
  • Assign the result to a variable res.
  • Print the result using the print() function and concatenation operator.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Tuple Elements Multiplication
# Using numpy module
 
import numpy as np
 
# initializing tup
test_tup = (7, 8, 9, 1, 10, 7)
 
# printing original tuple
print("The original tuple is : " + str(test_tup))
 
# Tuple Elements Multiplication
# Using numpy module
res = np.prod(test_tup)
 
# printing result
print("The product of tuple elements are : " + str(res))


Output:

The original tuple is : (7, 8, 9, 1, 10, 7)
The product of tuple elements are : 35280

Time complexity: O(n), The np.prod() function has a time complexity of O(n) as it iterates over all the elements of the input tuple once to calculate the product.
Auxiliary Space: O(1),  as the np.prod() function does not use any additional space to store the input and output values. The variable res created to store the output takes constant space.

Method: Using while() method-

  • result variable is initialized to 1 which will store the answer
  • i is initialized to 0 for while loop.
  • A while loop is used to iterate over the elements of test_tup using an index i.
  • In each iteration, the value at the ith index of test_tup is multiplied with the current value of result using *=, and the result is stored back in the result.
  • Then index is increased to go to the next iteration.
  • after while loop breaks the result is printed.

Python3




test_tup = (7, 8, 9, 1, 10, 7)
result = 1
i = 0
 
while i < len(test_tup):
    result *= test_tup[i]
    i += 1
 
print(result)


Output

35280

Time Complexity: O(n) as we are traversing to each element in a tuple so time complexity is O(N)

Space Complexity: O(1) because we are not using any extra space. So space complexity is O(1). 

RELATED ARTICLES

Most Popular

Recent Comments