0

I've got a list of numbers and I'm trying to loop through the list and change each number depending on a certain criteria. But my code doesn't change the list, its still the same when I print it again at the end. My code is below:

list = [[10.0, 4.0, 10.0, 10.0, 4.0, 0.0, 10.0, 10.0, 10.0, 4.0, 6.0]]

for x in list:
    if (x >= 0):
        x = 100
    if (x < 0):
        x = -100
print list

Looked at this and didnt solve the problem Looping over a list in Python

6
  • Read nedbatchelder.com/text/names.html Commented Feb 22, 2018 at 17:19
  • you have a list of a list of floats and you are treating it as if it was a list of floats. Apart from the fact that you are not changing the list item but rather x. Commented Feb 22, 2018 at 17:20
  • Yes, it's list inside a list. And then in loop list of int is compared with int. That's why its wrong. Commented Feb 22, 2018 at 17:21
  • @arundeepak Not only. That is why it raises an Error. But even if this was correct, this code would not change the list. Commented Feb 22, 2018 at 17:22
  • @Ev.Kounis ,yep :) Commented Feb 22, 2018 at 17:23

4 Answers 4

2

You've got two problems: one, your list is actually a list of lists; two, you are not assigning values to your list properly. Try something like this:

# Loop over the indices of the list within the list
for i in range(len(list[0])):
    if (list[0][i] >= 0):
        list[0][i] = 100
    else:
        list[0][i] = -100
print list

There are ways to make this more efficient and generalizable, but keeping your for loop style, the above code would work in your case.

For instance, you can also do this (essentially the same thing in list iteration form, and looping through each list in your list of list, in case you had more than just one):

[[100 if lst[i] >=0 else -100 for i in range(len(lst))] for lst in list]

for a list of multiple lists, this works as such:

old = [[10.0, 4.0, 10.0, 10.0, 4.0, 0.0, 10.0, 10.0, 10.0, 4.0, 6.0],[-2, 2, -2]]

new = [[100 if lst[i] >=0 else -100 for i in range(len(lst))] for lst in old]

>>> new
[[100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100], [-100, 100, -100]]

Also, it is generally bad style to call your list list, as list is already a built-in type in python.

Sign up to request clarification or add additional context in comments.

Comments

1

First problem You've got list of list of values [[]] instead just plain list []

Second problem You're not mutating the list, you need to directly change item by index

For the iterating with index, you can use enumerate(list)

Third comment In your case, maybe better use else instead of two if?

list = [10.0, 4.0, 10.0, 10.0, 4.0, 0.0, 10.0, 10.0, 10.0, 4.0, 6.0]

for i, x in enumerate(list):
    if (x >= 0):
        list[i] = 100
    else:
        list[i] = -100
print list

P.S. Maybe you shouldn't mutate the initial list? Maybe it's better to return new list

list = [10.0, 4.0, 10.0, 10.0, 4.0, 0.0, 10.0, 10.0, 10.0, 4.0, 6.0]

def return_new_list(list):
    new_list = []
    for x in list:
        if (x >= 0):
            new_list.append(100)
        else:
            new_list.append(-100)
    return new_list

print return_new_list(list)

Comments

0

using the code you posted..

list = [[10.0, 4.0, 10.0, 10.0, 4.0, 0.0, 10.0, 10.0, 10.0, 4.0, 6.0]]

for i in range(len(list[0])):
    if list[0][i] >= 0:
        list[0][i] = 100
    else:
        list[0][i] = -100
print(list)

Comments

-1

For 1 D list

list = [100 if x >= 0 else -100 for x in list]

In your question you have list of list. Means list inside list. In the above example, you can see I have iterated over each element and set the value on the basis of element.

If you have 2D list

list = [[100 if x >= 0 else -100 for x in items] for items in list]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.