Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Change values in one list by referring to a second one

kkhhhkk

Coder
I have two lists, we'll call them x and n, I've been trying to write a script that goes through the elements of list n and if they are negative then to change the corresponding index element of x to a positive value
I've tried
Code:
for i in range(0,len(n),1):
    if n[i]<0:
        x[i] = abs(x[i])
However this returns the error out of range, I've also tried:
Code:
p=[]
for i in range(0,len(n),1):
    if n[i]<0:
        p.append(i)
for i in p:
    x[p[i]] = abs(x[i])
but this returns the same error
 
Are you trying to do something like this?

Python:
n = [1,3,5,-5,8,3,-1]
x = [1,3,5,-5,8,3,-1]

for index, item in enumerate(n):
    if n[index] < 0:
        x[index] = abs(x[index])

print(n)
print(x)

Output:

Code:
[1, 3, 5, -5, 8, 3, -1]
[1, 3, 5, -5, 8, 3, -1]

After
[1, 3, 5, -5, 8, 3, -1]
[1, 3, 5, 5, 8, 3, 1]
 
Of course you can do it completely differently, as @menator01 suggests. There are always other ways.
But to simply fix the error you are getting, just use len(n)-1 instead of len(n). Recall that modern languages start counting at zero, so if you have an array n with 4 elements, the last element is n[3], not n[4].
 
Actually, range(num) evaluates from 0 to num-1 as you can see if you execute list(range(5)), so do not subtract 1 in the statement. Perhaps you should test to make sure the two lists are actually the same length.
 
Actually, range(num) evaluates from 0 to num-1 as you can see if you execute list(range(5)), so do not subtract 1 in the statement. Perhaps you should test to make sure the two lists are actually the same length.
Yes you are right, I was rather jumping to conclusions here 😳
It would have helped if the OP had indicated the line where the error is reported. I wonder why so many people do not bother to give the context of an error, leaving it to others to guess the problem spot as well as the problem itself.
 
One other thing I just learned (and it makes sense when you hear the explanation) - when you are working with slices in pandas DataFrames, both the start and end values are inclusive. The reason is that you can use slices with string (column header) values so it is clearer when you are selecting columns as ['Hobbyist':'Age'].
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom