Reputation:
For example, I have this that I opened from a .txt file:
{'fruit':['apple','pear'], 'veggies':['cucumber']}
How would I convert the items in the lists inside the whole dictionary into sets so it'll have the output:
{'fruit':{'apple','pear'}, 'veggies':{'cucumber'}}
Upvotes: 1
Views: 35
Reputation: 247
If you iterate through the keys in the dict, you can cast the lists to sets.
d = {'fruit': ['apple', 'pear'], 'veggies': ['cucumber']}
for key in d.keys():
s[key] = set(d[key])
This produces the output you want.
Upvotes: 0
Reputation: 33359
Iterate over all the keys in the dictionary, and reassign each key's value to be a set()
of its current value:
for key in mydict:
mydict[key] = set(mydict[key])
Upvotes: 1