dreamer dreamer - 3 months ago 11
Python Question

dictinary of dictionary in python

tags = {'stream', 'auth'}
tags['stream']= {}
tags["stream"]["path"]= ["/streams"]
tags['stream']['attribute']= ['id', 'secure', 'cpcode', 'format', 'event_pattern']


The above code throws an error:

tags["stream"]= {}
TypeError: 'set' object does not support item assignment


How to create a dictionary of dictionary of list?

Answer

{'stream', 'auth'} is a set literal, not a dictionary.

Use a dictionary literal:

tags = {'stream': {}, 'auth': {}}
tags["stream"]["path"]= ["/streams"]
tags['stream']['attribute']= ['id', 'secure', 'cpcode', 'format', 'event_pattern']

>>> type({'stream', 'auth'})
<type 'set'>
>>> type({'stream': {}, 'auth': {}})
<type 'dict'>
>>>
Comments