The problem you’re experiencing is due to a misunderstanding of lists, list-references, and possibly the concept of mutability.
In your case, you are binding the name user_data
to a list object. In other words, user_data
acts as a list-reference to the actual list object in memory.
When you say user_list.append(user_data)
, all you’re doing is appending a list-reference to user_list
. It doesn’t make a deep copy, or anything like that.
So, if you have two username-password-pairs, then you’ll have two list-references, both pointing to the same underlying list in memory. Any change you make to the list object through any of its references will be reflected in all other references, since they’re all just “pointing” to the same place.
>>> empty_list = []
>>> lists = [empty_list, empty_list]
>>> lists[0]
[]
>>> lists[1]
[]
>>> lists[0].append(1)
>>> lists[0]
[1]
>>> lists[1]
[1]
>>>
solved List in list changes but var in list not? [duplicate]