Open In App

Python – Raise elements of tuple as power to another tuple

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with records, we can have a problem in which we may need to perform exponentiation, i.e power of tuples. This problem can occur in day-day programming. Let’s discuss certain ways in which this task can be performed. 

Method #1: Using zip() + generator expression The combination of above functions can be used to perform this task. In this, we perform the task of power using generator expression and mapping index of each tuple is done by zip(). 

Step by step approach :

  1. Initialize two tuples test_tup1 and test_tup2 with values (10, 4, 5, 6) and (5, 6, 7, 5) respectively.
  2. Print the original tuples using the print() function and concatenation.
  3. Create a generator expression using the zip() function to iterate over the tuples test_tup1 and test_tup2 in parallel. This pairs up the corresponding elements of each tuple to form a new tuple for each index.
    For each tuple of paired elements, raise the first element to the power of the second element using the exponentiation operator **.
  4. Convert the resulting generator expression into a new tuple res using the tuple() function.
  5. Print the resulting exponentiated tuple res using the print() function and concatenation.

Python3




# Python3 code to demonstrate working of
# Tuple exponentiation
# using zip() + generator expression
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation
# using zip() + generator expression
res = tuple(ele1 ** ele2 for ele1, ele2 in zip(test_tup1, test_tup2))
 
# printing result
print("The exponentiated tuple : " + str(res))


Output : 

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)

Time complexity: O(n), where n is the length of the input tuples. 
Auxiliary space: O(n), where n is the length of the input tuples. 

Method #2: Using map() + pow The combination of above functionalities can also perform this task. In this, we perform the task of extending the logic of exponentiation using mul and mapping is done by map(). 

Python3




# Python3 code to demonstrate working of
# Tuple exponentiation
# using map() + pow
from operator import pow
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation
# using map() + pow 
res = tuple(map(pow, test_tup1, test_tup2))
 
# printing result
print("The exponentiated tuple : " + str(res))


Output : 

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)

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

Method #3 : Using numpy

Note: install numpy module using command “pip install numpy”

The numpy library in python can be used to perform exponentiation on tuples.

Python3




import numpy as np
 
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# Tuple exponentiation using numpy
res = tuple(np.power(test_tup1, test_tup2))
 
# Print the result
print("The exponentiated tuple : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The exponentiated tuple : (100000, 4096, 78125, 7776)

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4 : Using operator.pow() method

Approach 

  1. Initiated a for loop
  2. Raised first tuple elements to the power of second tuple elements and appended to output list
  3. Displayed the output list

Python3




# Python3 code to demonstrate working of
# Tuple exponentiation
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation
res=[]
for i in range(0,len(test_tup1)):
    import operator
    res.append(operator.pow(test_tup1[i],test_tup2[i]))
# printing result
print("The exponentiated tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using list comprehension:

Iterate over the indices of the tuples using range(len(test_tup1)) and calculate the exponential of corresponding elements using the ** operator. The result is stored in a list and then converted to a tuple using the tuple() function.

Python3




# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation using list comprehension
res = tuple([test_tup1[i] ** test_tup2[i] for i in range(len(test_tup1))])
 
# printing result
print("The exponentiated tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 6: Using list comprehension is using the built-in map() function along with lambda function:

The map() function applies the lambda function to each corresponding element of the tuples test_tup1 and test_tup2. The resulting values are then used to create a new tuple res.

Python3




# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation using map() and lambda function
res = tuple(map(lambda x, y: x ** y, test_tup1, test_tup2))
 
# printing result
print("The exponentiated tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : (100000, 4096, 78125, 7776)

Time complexity: O(n), where n is the length of the tuples test_tup1 and test_tup2. This is because both approaches iterate over the tuples once.
Auxiliary space: O(n) because it creates a new list using list comprehension before converting it to a tuple. 

Method 7: Using Recursive method.

Algorithm for the recursive tuple_exponentiation function:

  1. If the length of tup1 is 0, return an empty list.
  2. Otherwise, calculate the exponentiation of the first elements of tup1 and tup2 using the pow() function from the operator module, and add it to the result of calling tuple_exponentiation with the remaining elements of tup1 and tup2.
  3. Return the resulting list.

Python3




def tuple_exponentiation(tup1, tup2):
    if len(tup1) == 0:
        return []
    else:
        import operator
        return [operator.pow(tup1[0], tup2[0])] + tuple_exponentiation(tup1[1:], tup2[1:])
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
res = tuple_exponentiation(test_tup1, test_tup2)
 
# printing result
print("The exponentiated tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]

The time complexity of this algorithm is O(n), where n is the length of the tuples, since the function needs to process each element in both tuples exactly once. 

The space complexity is also O(n), due to the use of recursion and the creation of the result list. However, the constant factors involved in the space complexity will be larger than for the iterative solution, since each recursive call requires additional memory for the function call stack.

Method 8: using a for loop

 Using a for loop and append the result of each element in the new list.

Python3




def tuple_exponentiation(tup1, tup2):
    result = []
    for i in range(len(tup1)):
        result.append(tup1[i] ** tup2[i])
    return result
 
# initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tup1))
print("The original tuple 2 : " + str(test_tup2))
 
# Tuple exponentiation using map() and lambda function
res = tuple_exponentiation(test_tup1, test_tup2)
 
# printing result
print("The exponentiated tuple : " + str(res))


Output

The original tuple 1 : (10, 4, 5, 6)
The original tuple 2 : (5, 6, 7, 5)
The exponentiated tuple : [100000, 4096, 78125, 7776]

Time complexity: O(n) where n is the length of the tuples.
Auxiliary space: O(n) where n is the length of the tuples, as we create a new list to store the result.

Method 9: Using itertools:

1.Import the starmap function from the itertools module.

2.Define two tuples test_tup1 and test_tup2 with the numbers to be exponentiated.

3.Print the original tuples.

4.Use the zip function to create an iterable of tuples with the corresponding elements of test_tup1 and test_tup2. Then, use starmap and pow to apply the exponentiation operation to each pair of elements in the iterable. Finally, create a tuple with the results.

5.Print the resulting tuple.

Python3




from itertools import starmap
 
# Initialize tuples
test_tup1 = (10, 4, 5, 6)
test_tup2 = (5, 6, 7, 5)
 
# Print original tuples
print("The original tuple 1: " + str(test_tup1))
print("The original tuple 2: " + str(test_tup2))
 
# Exponentiation using itertools and starmap
res = tuple(starmap(pow, zip(test_tup1, test_tup2)))
 
# Print results
print("Exponentiation using itertools and starmap: " + str(res))
# This code is  contributed by Vinay pinjala.


Output

The original tuple 1: (10, 4, 5, 6)
The original tuple 2: (5, 6, 7, 5)
Exponentiation using itertools and starmap: (100000, 4096, 78125, 7776)

Time complexity: O(n), where n is the length of the tuples
Space complexity: O(n), as the result is stored in a new tuple of length n



Similar Reads

Raise a square matrix to the power n in Linear Algebra using NumPy in Python
In this article, we will discuss how to raise a square matrix to the power n in the Linear Algebra in Python. The numpy.linalg.matrix_power() method is used to raise a square matrix to the power n. It will take two parameters, The 1st parameter is an input matrix that is created using a NumPy array and the 2nd parameter is the exponent n, which ref
3 min read
Raise Chebyshev series to a power using NumPy in Python
In this article, we will cover how to raise a Chebyshev series to power in Python using NumPy. polynomial.chebyshev.chebpow method The Chebyshev polynomials are like the other classical orthogonal polynomials which can be defined from a variety of starting locations. Here, we will see how to raise a Chebyshev series to power in Python. The NumPy pa
3 min read
Raise a Hermite series to a power in Python
The Hermite polynomials, like the other classical orthogonal polynomials, can be defined from a variety of starting locations. In this article let's see how to Raise a Hermite series to a power in Python. The NumPy package provides us polynomial.hermite.hermpow() method to raise a Hermite series. Syntax: polynomial.hermite.hermpow(c, pow, maxpower=
3 min read
Raise a Hermite_e series to a power using NumPy in Python
In this article, we will cover how to raise a Hermite_e series to power in Python using NumPy. Example Input: [1,2,3,4,5] Output: [3.66165553e+08 1.13477694e+09 3.40357434e+09 3.15464455e+09 4.62071689e+09 2.26002499e+09 2.15384684e+09 6.43945096e+08 4.48843239e+08 8.58196000e+07 4.66275200e+07 5.62768800e+06 2.48058600e+06 1.73880000e+05 6.3900000
2 min read
Raise a File Download Dialog Box in Python
Raising a File Download Dialog Box for end-user to download files like pdf, media-objects, documents, etc in Python can be done by the use of HTTP Header. It comes in handy where there is a need to develop a feature where instead of showing the files in the browser, the file contains is automatically downloaded.For instance, if you need a file say
2 min read
Python Raise Keyword
In this article, we will learn how the Python Raise keyword works with the help of examples and its advantages. Python Raise KeywordPython raise Keyword is used to raise exceptions or errors. The raise keyword raises an error and stops the control flow of the program. It is used to bring up the current exception in an exception handler so that it c
3 min read
Python | Sort tuple list by Nth element of tuple
Sometimes, while working with Python list, we can come across a problem in which we need to sort the list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using sort(
8 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + enumerate() This task
8 min read
Python - Convert Tuple String to Integer Tuple
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be used to perform this task. In this, we perform th
7 min read
Python - Convert Tuple to Tuple Pair
Sometimes, while working with Python Tuple records, we can have a problem in which we need to convert Single tuple with 3 elements to pair of dual tuple. This is quite a peculiar problem but can have problems in day-day programming and competitive programming. Let's discuss certain ways in which this task can be performed. Input : test_tuple = ('A'
10 min read
Practice Tags :