So if the input is [3, 2, 5]
, the output would be [1, 2, 25]
, and if the input was [5, 10, 20]
, the output would be [1, 10, 400]
(5^0, 10^1, 20^2)

- 12,464
- 7
- 65
- 73

- 11
- 1
-
4Please show what you've tried in order to solve this problem yourself. – roganjosh Feb 05 '18 at 21:31
-
Have you tried looping over the list, and raising each element to the indices power? – juanpa.arrivillaga Feb 05 '18 at 21:33
-
1use list comprehension and the enumerate function – Keith Feb 05 '18 at 21:34
5 Answers
You could just use a map between your input list and a range.
def raise_power(input_list):
return list(map(lambda x,y: x**y, input_list, range(len(input_list))))
Or a more straightforward way (suggested by @roganjosh) to put it could be :
def raise_power(input_list):
return [i**x for x, i in enumerate(input_list)]
Both have no significant difference in computation time. (see comments)
-
6Seems overkill when you could just do `a = [i**x for x, i in enumerate([5, 10, 20])] ` – roganjosh Feb 05 '18 at 21:38
-
Sure, your solution works too and is more pythonic :-). No idea about speed though – arnaud Feb 05 '18 at 21:40
-
1Sorry, first timings were incorrect. But the corrected timings show no difference. With a list of 1000 values: mine: `7.34 ms ± 70.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)` and yours: `7.38 ms ± 9.59 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)` – roganjosh Feb 05 '18 at 21:59
-
-
1No, I meant what I put. Your change to my code does not give the correct answer. – roganjosh Feb 06 '18 at 00:25
a very simple way is use a loop like following
for i in range(0,len(input)):
print(input[i]**i)

- 732
- 1
- 8
- 18
Your own solution is neither correct nor pythonic . Here is your code will fail :
So if my input is :
3 2 5 3 2 5
output should be :
[1, 2, 25, 27, 16, 3125]
But your code will give :
[1, 2, 25, 1, 2, 25]
Because after 3,2,5 when again 3 comes in the list then index(i) will choose first 3 in the list not second 3 or third or any. So index will always choose first appear numbers and hence you will get the wrong output.
The right way is use enumerate :
print([j**i for i,j in enumerate([int(i) for i in input().split()])])
output:
[1, 2, 25, 27, 16, 3125]

- 1
- 1

- 12,007
- 7
- 50
- 88
You need to split the input into a list, then use enumerate to get the index position and the value. Since the value will be a string, use int to convert and then raise it to the power.
x = input('Enter the list of numbers : ')
y = [int(i) for i in x.split(',')]
print ([int(a)**i for i,a in enumerate(y)])
Code output will be:
Enter the list of numbers : 3,2,5
[1, 2, 25]

- 8,417
- 2
- 13
- 33
I actually figured it out myself! I'm sure you are all very proud. The below code will produce the output I'm looking for. Thank you though!
firstlist = [int(x) for x in input().split()] newlist = [i ** (int(firstlist.index(i))) for i in firstlist]

- 11
- 1
-
You don't really need to lookup for finding the right indices as you know they'll come sorted... thus the use of `enumerate` suggested by @roganjosh below – arnaud Feb 05 '18 at 21:41
-
7