Hi, I have a list of strings (keys), and I'm trying to create a dictionary out of that list of keys. The values should be individual empty lists.
I tried
myDict = dict.fromkeys(keys, [])
, but that was very deceptive. It turns out every list in the dict is a reference to the same list, so editing myDict["str1"] edits every other value at the same time.
Right now I'm using
dict.fromkeys(keys)
for key in keys:
dict[key] = []
Question
Andre S. Veteran
Hi, I have a list of strings (keys), and I'm trying to create a dictionary out of that list of keys. The values should be individual empty lists.
I tried
, but that was very deceptive. It turns out every list in the dict is a reference to the same list, so editing myDict["str1"] edits every other value at the same time.
Right now I'm using
, but is there no cleaner way?
Thanks.
Link to comment
Share on other sites
0 answers to this question
Recommended Posts