Dmitriy_kzn
Dmitriy_kzn

Reputation: 578

update all items in a list using function in python

Could you please tell what i'm doing wrong and how to fix it.

Thanks

I have a function.

def out(some_list):
    test_list = [1,2,3,4]
    result = []

    for i in some_list:
        if i == 1:
            test_list = [0,0,0,0]
        else:
            test_list = test_list

        result.append(test_list)

    return result

if we print it out it will return:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

I need to return

[[0, 0, 0, 0], [1,2,3,4], [1,2,3,4], [1,2,3,4]]

Upvotes: 0

Views: 6113

Answers (2)

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

This is because the list you are passing in this function has 1 as the value of the first element. For example:

out([1,2,3,4]) # ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Going through your code step by step:

test_list = [1,2,3,4]
result = []

for i in some_list:           # The value of each element in some_list
    if i == 1:                # If the value is "1" set test_list: [0,0,0,0]
        test_list = [0,0,0,0]
    else:
        test_list = test_list # Otherwise set test_list to itself (doing nothing)

    result.append(test_list)

for i in some_list: 

The for loop value of i is the value of the element you are on in some_list, it is not the index or position of the element we are on in the list (as it appears this question intends)

    if i == 1:
        test_list = [0,0,0,0]

If the value is 1, then test_list will be set to [0,0,0,0]. Once this is hit only the value [0,0,0,0] will be appended to result. So if the first element is 1 then you will only see the value [0,0,0,0] in the result, otherwise you will see [1,2,3,4] until the loop hits where the value in the list some_list is 1.

Here are some examples:

out([0,1,2,3]) # [[1, 2, 3, 4], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([1,2,3,4]) # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
out([2,2,5,1]) # [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [0, 0, 0, 0]]

Hopefully this makes it more clear why you are getting that result.


Edit

In terms of your updated question whats happening here is that when you call .append(fig) is simply a copy of the reference to the fig in memory. Basically whenever it changes all the copies that you appended will change as well. There are two ways you can handle this, first would be have the variable fig be defined in the scope of your loop, this way it's a new and different variable on each loop:

 for i in test_list:
   fig = [2, 1]  # <== In the scope of the loop, so each fig is it's on variable
   ...

The second way is you could append fig[:], which means it will copy the array fig as a new array and pass that in for append:

for i in test_list:

  if i == '0':
      fig[0] = off
      fig[1] = off
  elif i == '1':
      fig[0] = off
      fig[1] = on

  new_list.append(fig[:]) # <== Copy the array fig and append that value

Upvotes: 1

Samantha
Samantha

Reputation: 849

It is because you set test_list = [0,0,0,0] so even in test_list = test_list it is keeping the result from setting it to [0,0,0,0]

try using

def out(some_list):
test_list = [1,2,3,4]
result = []

for i in some_list:
    if i == 1:
        result.append([0,0,0,0])
    else:
        result.append(test_list)

return result

Upvotes: 0

Related Questions