# 5 Best Ways to Return the Bases When First Array Elements Are Raised to Powers From Second Array in Python

Rate this post

π‘ Problem Formulation: You have two arrays β one representing base numbers and the other representing exponents. The challenge is to write a Python function that takes these arrays and returns the results of raising each base to its corresponding exponent. For example, given [2, 3, 4] as bases and [3, 2, 1] as exponents, the desired output would be [8, 9, 4].

## Method 1: Using a for loop

This method involves iterating through both arrays simultaneously with a `for` loop and raising each base to its corresponding exponent. The `zip` function is useful here in combining both arrays for iteration.

Here’s an example:

```bases = [2, 3, 4]
exponents = [3, 2, 1]
results = []

for base, exponent in zip(bases, exponents):
results.append(base ** exponent)```

Output: [8, 9, 4]

This code first initializes an empty list named `results`. It then iterates over the paired elements of `bases` and `exponents` using the `zip` function. In each iteration, the base is raised to the power of the exponent, and the result is appended to the `results` list.

## Method 2: Using list comprehension

List comprehension offers a concise and readable way to create a new list by performing operations on each item in an existing list. This method uses a single line of code to perform the same operation as Method 1.

Here’s an example:

```bases = [2, 3, 4]
exponents = [3, 2, 1]
results = [base ** exponent for base, exponent in zip(bases, exponents)]```

Output: [8, 9, 4]

The list comprehension iterates through each pair of base and exponent in the zipped list and computes the power operation. The result is a new list of powers which is assigned to the variable `results`.

## Method 3: Using map and lambda

A functional approach using `map` and `lambda` functions, which applies a lambda function over elements of specified iterators. This method is suitable when you want to apply a single operation over a list of items.

Here’s an example:

```bases = [2, 3, 4]
exponents = [3, 2, 1]
results = list(map(lambda base, exponent: base ** exponent, bases, exponents))```

Output: [8, 9, 4]

The `map` function applies a lambda function, which takes base and exponent as arguments and returns their power, to each pair of base and exponent from the lists `bases` and `exponents`. Since `map` returns a map object, it is converted to a list.

## Method 4: Using NumPy library

NumPy is a powerful numerical computation library that makes array operations more efficient. This method leverages the `np.power()` function, taking advantage of NumPyβs vectorization capabilities to compute powers.

Here’s an example:

```import numpy as np

bases = np.array([2, 3, 4])
exponents = np.array([3, 2, 1])
results = np.power(bases, exponents)```

Output: [8 9 4]

This snippet first converts the lists `bases` and `exponents` into NumPy arrays. It then uses the `np.power()` function, which computes each element in the first array raised to powers from the second array efficiently and returns the results as a NumPy array.

## Bonus One-Liner Method 5: Using itertools and reduce

Combining `itertools.starmap` with `functools.reduce` can offer a one-liner solution that is efficient and functional in nature. This leverages advanced Python features to achieve the same result.

Here’s an example:

```from itertools import starmap
from functools import reduce

bases = [2, 3, 4]
exponents = [3, 2, 1]
results = list(starmap(pow, zip(bases, exponents)))```

Output: [8, 9, 4]

The `starmap` function from the `itertools` module applies the built-in `pow` function to every pair obtained from `zip(bases, exponents)`. The result is then converted to a list since `starmap` returns an iterator.

## Summary/Discussion

• Method 1: For loop with `zip`. Straightforward and easy to understand. Can be slower for large arrays due to explicit looping.
• Method 2: List comprehension. Compact and Pythonic. Efficiency is similar to a for loop, and it’s best suited for small to medium-sized arrays.
• Method 3: Map and lambda. Offers a clear functional style. May be less readable to those unfamiliar with functional programming paradigms.
• Method 4: NumPy library. Very efficient, especially for large arrays, leveraging vectorization. Requires an external library, thus the code is not purely Pythonic.
• Method 5: Itertools and reduce. Powerful and concise. May not be as readable and requires understanding of Python’s functional tools.