I'm trying to update a Set in a nested Dictionary, problem is it adds a new Set instead of updating the current Set the 2nd time.
building = {}
building.update({'APT-14D':{}})
building['APT-14D'].setdefault('amenities', set()).add(('hot-water','gas-stove','non-smoke'))
building['APT-14D'].setdefault('amenities', set()).add(('hot-water','non-stove','non-smoke'))
I receive the following:
{'APT-14D': {'amenities': {('hot-water', 'non-stove', 'non-smoke'), ('hot-water', 'gas-stove', 'non-smoke')}}}
instead of:
{'APT-14D': {'amenities': {('hot-water', 'non-stove', 'non-smoke')}}}
How would I update the current value instead of making a new Set inside the nested dictionary?
P.S. The different values are non-stove
and gas-stove
You aren't making a new set, you are adding a different tuple to your set. If you want to add each of the individual elements of your tuple to the set, use .update
:
In [4]: building = {}
...: building.update({'APT-14D':{}})
...: building['APT-14D'].setdefault('amenities', set()).update(('hot-water','gas-stove','non-smoke'))
...: building['APT-14D'].setdefault('amenities', set()).update(('hot-water','non-stove','non-smoke'))
...:
In [5]: building
Out[5]:
{'APT-14D': {'amenities': {'gas-stove',
'hot-water',
'non-smoke',
'non-stove'}}}
Rereading your question, I've notice that isn't even really what you want. It seems now that you don't even want a set, just a tuple, and just replace that tuple with whatever new value comes:
In [16]: building = {'APT-14D':{}}
In [17]: building['APT-14D']['amenities'] = ('hot-water','gas-stove','non-smoke')
In [18]: building['APT-14D']['amenities'] = ('hot-water','non-stove','non-smoke')
In [19]: building
Out[19]: {'APT-14D': {'amenities': ('hot-water', 'non-stove', 'non-smoke')}}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments