How to edit list elements in for loop – python

Question:

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

Asked By: Stussy

||

Answers:

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.

Answered By: sacuL

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]
Answered By: Ashish

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)
Answered By: alvarosps

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)
Answered By: Dmitry Birin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.