double assign in python results in identical lists even when different operation is done. Why? -
this question has answer here:
- how clone or copy list? 16 answers
i'm facing situation can't crack logic behind.
i have piece of code
user_id = user_email = [] id, email in users: # users tuple of tuples looks ((a,b),(c,d)...) user_id.append(id) user_email.append(email)
when check result, found user_id
== user_email
when assign them separately, can correctly id's , email's, instead of 2 identical lists contain both id's , email's
i'm wondering what's logic behind double assign , causes phenomenon happen.
when set
user_id = user_email = []
you link user_id , user_email 1 list. when list changes both variables change.
set these variables separately.
user_id = [] user_email = []
this can confusing if coming language c. think of variable in python name not variable.
Comments
Post a Comment