0

I don't know if I miss something here but don't understand why does the below list changes after using append() function. First I send a list(which contains lists) to 2 different functions.

First function just returns a random list in all non-empty lists. Second function returns a random list that if the current list(which is the one returned from the first function)'s last element lower than the other list in state or if the list is empty.

import random
def fooOne(state):
    lst = [x for x in state if len(x) > 0]
    return random.choice(lst)

def fooTwo(state,current):
    lst2 = [x for x in state if len(x) == 0 or x[-1] > current[-1]]
    return random.choice(lst2)

state = [[3, 2, 1],
         [],
         []]

cur = fooOne(state)
temp = fooTwo(state,cur)

print("State before append(): {}".format(state))
temp.append(cur.pop())
print("State after append(): {}".format(state))

Both functions returns different lists. I assign these lists to two different variables called cur and temp. So both cur and temp are also lists.

After temp.append(cur.pop()) line, original state list changes. It'll changes like I used that line on the original list. But I'm making this operation on two different lists.

Output:

State before append(): [[3, 2, 1], [], []]
State after append(): [[3, 2], [], [1]]

Why does the original list changes while I'm using append() on another list? These lists are just some parts of the original list, why modifying them(while assigning them to new variables) affects the original list?

GLHF
  • 3,835
  • 10
  • 38
  • 83
  • 1
    Does this answer your question? [List of lists changes reflected across sublists unexpectedly](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) PS: Loved your profile. – Celius Stingher Feb 12 '20 at 17:27
  • 2
    You didn't change a different list; you changed the same list under a second name. – Prune Feb 12 '20 at 17:28
  • @CeliusStingher No I already checked that question. I'm not modfying the original list, I assign them to another variables. – GLHF Feb 12 '20 at 17:29
  • I can only see 1 list "state" – Wonka Feb 12 '20 at 17:29
  • You return a shallow copy of the list, i.e. the most internal lists are not copied. Try: `lst2 = [list(x) for x in state if len(x) == 0 or x[-1] > current[-1]]` and see it working as expected? – Jacques Gaudin Feb 12 '20 at 17:32
  • 1
    Your functions pick sublists out of `state` without making any copies. When you perform `temp.append(cur.pop())` you are modifying one of those sublists which are still in `state`. Therefore printing `state` shows you what has changed. – quamrana Feb 12 '20 at 17:33
  • Use the `id()` method to see whether you have a different list. Just as in the duplicate, you made other references to `state`; giving those references other explicit names doesn't change the fact that it's still a reference to `state`. – Prune Feb 12 '20 at 17:37
  • The answer is that you aren't modifying two lists, you're modifying one list with two references. One variable can have two references; modifying one modifies them both. – Ken Kinder Feb 13 '20 at 08:46

0 Answers0