I have a dictioanry that has a key with more than one value:
JavaScript
x
2
1
{'bob': {"flying pigs"}, 'sam':{"open house", "monster dog"}, 'sally':{"glad smile"}}
2
I would like the dictionary on output to look like:
JavaScript
1
5
1
bob, flying pigs
2
sam, open house
3
sam, monster dog
4
sally, glad smile
5
My biggest struggle is to get the sam key to print the values on their own line.
Advertisement
Answer
you can use 2 for loops to first iterate on keys and value (dictionary) and the 2nd one to iterate on the set (dictionary values).
JavaScript
1
6
1
mydict = {'bob': {"flying pigs"}, 'sam':{"open house", "monster dog"}, 'sally':{"glad smile"}}
2
3
for key, value in mydict.items():
4
for item in value:
5
print(key, item, sep=', ')
6
Output:
JavaScript
1
5
1
bob, flying pigs
2
sam, open house
3
sam, monster dog
4
sally, glad smile
5