# Python – Raise elements of tuple as power to another tuple

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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next