Home > Software design >  How would I raise integers in a list to successive powers? (python)
How would I raise integers in a list to successive powers? (python)

Time:01-04

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.

CodePudding user response:

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]
  • Related