0

Here is an example of what I'm trying to do

profData = []
currentDic = {}

currentDic['name'] = 'Mike'
currentDic['job'] = 'Bartender'
currentDic['company'] = 'Big Bar'

profData.append(currentDic)
currentDic.clear()

currentDic['name'] = 'John'
currentDic['job'] = 'Server'
currentDic['company'] = 'Red Robin'

profData.append(currentDic)
currentDic.clear()


print(profData)
print(currentDic)

And for some reason I'm getting this result

[{}, {}]
{}

I want to re-use currentDic over and over to insert dictionaries into the profData list. Any thoughts?

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
couch_coder
  • 11
  • 1
  • 3
  • You are indeed re-using `currentDic` while that's not what you wanted. Initialize a new one using `currentDic = {}` instead of `.clear()`ing the existing one. – Selcuk Jul 16 '21 at 02:37
  • That's so odd. I'm surprised that it clears it even once it's been appended to profData. You're definitely right though, just tried it and that works. Thanks! – couch_coder Jul 16 '21 at 02:42
  • 2
    Not so odd. `.clear()` will not initialize a new object; it will simply clear the contents of the existing one. You are basically appending the same object (not a copy of it; the object itself) twice to the list. See [this question](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) for a similar mutable object issue. – Selcuk Jul 16 '21 at 02:46
  • 2
    Does this answer your question? [Creating a list of dictionaries results in a list of copies of the same dictionary](https://stackoverflow.com/questions/11492656/creating-a-list-of-dictionaries-results-in-a-list-of-copies-of-the-same-dictiona) – Mad Physicist Jul 16 '21 at 04:38
  • 1
    Just to re-emphasize what @Selcuk said, there is only one list and only one dictionary in that entire program. You have three references to that one dictionary, but when you change the dictionary, you see it everywhere. – Tim Roberts Jul 16 '21 at 04:42

1 Answers1

0

As @Selcuk mentioned In python, when you create a variable and assign a value, your variable is a reference to that value. If the same value is assigned to another variable, another reference is created for that value instead of storing the value at a new memory address and assigning it to the variable.

In your program, you create a dictionary and append it to a list. The dictionary inside the list will hold the same reference to the dictionary that was created above. It is similar to pass-by-reference.

To avoid the problem you are facing what can you do is pass a copy of the dictionary to the list.

import copy

profData = []
currentDic = {}

currentDic['name'] = 'Mike'
currentDic['job'] = 'Bartender'
currentDic['company'] = 'Big Bar'

profData.append(copy.copy(currentDic)) # Use copy.deepcopy if your dictionary is nested
currentDic.clear()

currentDic['name'] = 'John'
currentDic['job'] = 'Server'
currentDic['company'] = 'Red Robin'

profData.append(currentDic)
currentDic.clear()


print(profData)
print(currentDic)

Output:

[{'name': 'Mike', 'job': 'Bartender', 'company': 'Big Bar'}, {}]
{}
bumblebee
  • 1,811
  • 12
  • 19