• 0

create dict in python


Question

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] = []

, but is there no cleaner way?

Thanks.

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.