How would I raise integers in a list to successive powers? (python)

73 Views Asked by At

Beginner here.

I want to raise elements(integers) in the list to the power of x+1, but am stuck.

For example:

  • Lst = [a, b, c, d, e]
  • a^x, b^(x+1), c^(new x + 1), d^(new x + 1), and so forth..

Another example:

  • Lst = [8, 7, 8, 5, 7]
  • x = 2

[8^2, 7^3, 8^4, 5^5, 7^6] is the output I would like..

Thank you!!!

I tried various for-loops to iterate into the elements of the list; pow(iterable, x+1). I've been at it for a few days but can't figure it out. I am also new to programming in general.

2

There are 2 best solutions below

0
Andrej Kesely On BEST ANSWER

Try:

lst = [8, 7, 8, 5, 7]
x = 2

out = [v ** i for i, v in enumerate(lst, x)]
print(out)

Prints:

[64, 343, 4096, 3125, 117649]
0
Terry Spotts On

itertools.count can help you increment x here:

from itertools import count

lst = [8, 7, 8, 5, 7]
x = count(2)

print([ele ** next(x) for ele in lst])

Output

[64, 343, 4096, 3125, 117649]