Reputation: 3
Below I have the code.
At ex2
and ex3
, lst[2]
and lst[3]
show the different output.
For example:
At ex3, lst[2] shows the output of 5 which is correct but
at ex2, lst[2] shows the output of 4 which is not correct because lst[2] should be added by 2, not by one
Why is that? What did I type wrong? I am new to python so any help would be nice. Thanks
def add_indexes(lst):
for x in range(len(lst)):
if lst[x] == lst[0]:
lst[x] += 0
elif lst[x] == lst[1]:
lst[x] += 1
elif lst[x] == lst[2]:
lst[x] += 2
elif lst[x] == lst[3]:
lst[x] += 3
elif lst[x] == lst[4]:
lst[x] += 4
return lst
ex1 = [0, 0, 0, 0, 0]
ex2 = [1, 2, 3, 4, 5]
ex3 = [5, 4, 3, 2, 1]
print(add_indexes(ex1))
print(add_indexes(ex2))
print(add_indexes(ex3))
Upvotes: 0
Views: 112
Reputation: 11
I see a lot of correct answers here,but I wanted to point out the mistake you have made.
From what I could see ,you're trying the check for each individual case of the index in your list and adding the corresponding index to the value stored at that particular list. Considering from this point of view ,the "if" condition you have choose:
if lst[x] == lst[0]:
elif lst[x] == lst[1]:
elif lst[x] == lst[2]:
elif lst[x] == lst[3]:
elif lst[x] == lst[4]:
This checks if the value at the current index is equal to the value at each indices 0,1,2,3,4. This makes the list dynamic as explained by @CanciuCostin.
The right way to do it will look something like this:
for x in range(len(lst)):
if x == 0:
lst[x] += 0
elif x == 1:
lst[x] += 1
elif x == 2:
lst[x] += 2
elif x == 3:
lst[x] += 3
elif x == 4:
lst[x] += 4
But this is an inefficient way.Thanks to @Sai Sreenivas for his improved efficient code to tackle your problem.
Upvotes: 0
Reputation: 19307
I would recommend doing this another way -
[np.sum(i) for i in enumerate(ex2)]
[1, 4, 6, 9, 13]
[np.sum(i) for i in enumerate(ex2)]
[5, 6, 7, 8, 9]
Upvotes: 0
Reputation: 1700
Mistake:
You are changing the values iteratively, so when x = 2 already lst[1] is 3, so it's adding only 1 to lst[2]
I think you're trying to add numbers based on their position in the list. You can simply do this:
ex1 = [0, 0, 0, 0, 0]
for i in range(len(ex1)):
ex1[i] += i
print(ex1)
output:
[0, 1, 2, 3, 4]
Upvotes: 0
Reputation: 1908
The reason is that you are first updating the values for lst[1] from 2 to 3, meaning that at the next iteration lst[1] == lst[2] will apply instead.
Starting
lst = [1,2,3,4,5]
Iteration 0:
x=0 => nothing changes
Iteration 1:
x=1 => lst[1] == lst[1] => lst[1]+=1 => lst[1] =3
Iteration 2:
x=2 => lst[2] == lst[1] => lst[2]+=1 => lst[2] =3
For your requirement you can simply use list comprehension:
ex2 = [1, 2, 3, 4, 5]
result = [x+ind for ind, x in enumerate(ex2)]
Output:
>>> result
[1, 3, 5, 7, 9]
Upvotes: 1
Reputation: 20765
From the OP's comment:
Given a list of numbers, create a function which returns the list but with each element's index in the list added to itself. This means you add 0 to the number at index 0, add 1 to the number at index 1, etc...
The function can be as simple as this:
def add_indexes(lst):
for x in range(len(lst)):
if lst[x] += x:
return lst
Upvotes: 0
Reputation: 905
That's because you increase the list on the fly, after 2 loops you have something like that:
lst = [1,3,3,4,5]
so condition:
elif lst[x] == lst[1]:
lst[x] += 1
gets real and you increase 3 by 1 instead of increasing by 2. Try adding operation on a copy of the list or on an empty list and append items.
Upvotes: 0