0

I make dictionary in python and append it into a list,after that I updated my keys value and then append it into my list but it remove my previous record and overwrite with the new record. How can I get out of it please guide me.

dic={
"name":"karan", 
"password":"karan123"
}

List1=[]

List1.append(dic)

dic.update({"name":"Zakir","password"="Zakir123"

List1.append(dic)

Result:

[{"name":"zakir","password":"zakir123"},{"name":"zakir","password":"zakir123"},]

But I want it like this:

[{"name":"karan","password":"laran123"},{"name":"zakir","password":"zakir123"},]
umläute
  • 28,885
  • 9
  • 68
  • 122
  • Your list contains a reference to the original object, not a unique copy of the dictionary. You can use deepcopy to append a copy of the dictionary, or just create a new dictionary for each entry that you want to add to the list – G. Anderson Jan 24 '20 at 20:55
  • 1
    Give you're replacing all the keys in `dic`, why not just assign a brand-new `dict` to it? – ShadowRanger Jan 24 '20 at 21:27

0 Answers0