Update a Set inside a nested Dictionary

Daniel Plas Rivera

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

juanpa.arrivillaga

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Eliminate items of a nested list, inside a nested dictionary

From Java

Swift 5 How do we parse a Dictionary nested inside a Dictionary, nested inside another Dictionary?

From Dev

Filtering nested lists inside a dictionary in Swift

From Dev

Scanning and replacing values inside nested dictionary

From Dev

How to check a value is inside nested dictionary swift

From Dev

Python get nested dictionary value inside of list

From Dev

Angular JS update html with nested dictionary

From Dev

Angular JS update html with nested dictionary

From Dev

Best practice to recursively update a nested dictionary?

From Dev

How to update entries in a table within a nested dictionary?

From Dev

Can't update a value in a Dictionary inside an Array?

From Dev

How to UPDATE/SET inside a function

From Dev

update a set inside a dict comprehension?

From Dev

Create a single Dictionary from a Nested Dictionary to Update MySQLdb

From Dev

Obtaining list (or set) of unique values in nested dictionary

From Dev

Python: Set a string as the default of a nested dictionary?

From Dev

Getting values from several set inside dictionary

From Dev

Elasticsearch: remove/update field inside nested object

From Dev

MongoDB update inside 2 nested arrays of objects

From Dev

update value inside custom nested plist

From Dev

how to update nested document inside array in elasticsearch

From Dev

Update state of array nested inside object (ReactJS)

From Dev

swift dictionary nested array manipulation - cannot mutate nested array inside dictionary

From Dev

swift dictionary nested array manipulation - cannot mutate nested array inside dictionary

From Dev

Check each value inside a nested array of a dictionary Swift

From Dev

how to split a nested dictionary inside a column of a dataframe into new rows?

From Dev

Populate NSDictionary in tableview from a nested plist array (inside a dictionary)

From Dev

matlab update two set of subplots inside a loop

From Dev

Unable to update nested dictionary value in multiprocessing's manager.dict()

Related Related

  1. 1

    Eliminate items of a nested list, inside a nested dictionary

  2. 2

    Swift 5 How do we parse a Dictionary nested inside a Dictionary, nested inside another Dictionary?

  3. 3

    Filtering nested lists inside a dictionary in Swift

  4. 4

    Scanning and replacing values inside nested dictionary

  5. 5

    How to check a value is inside nested dictionary swift

  6. 6

    Python get nested dictionary value inside of list

  7. 7

    Angular JS update html with nested dictionary

  8. 8

    Angular JS update html with nested dictionary

  9. 9

    Best practice to recursively update a nested dictionary?

  10. 10

    How to update entries in a table within a nested dictionary?

  11. 11

    Can't update a value in a Dictionary inside an Array?

  12. 12

    How to UPDATE/SET inside a function

  13. 13

    update a set inside a dict comprehension?

  14. 14

    Create a single Dictionary from a Nested Dictionary to Update MySQLdb

  15. 15

    Obtaining list (or set) of unique values in nested dictionary

  16. 16

    Python: Set a string as the default of a nested dictionary?

  17. 17

    Getting values from several set inside dictionary

  18. 18

    Elasticsearch: remove/update field inside nested object

  19. 19

    MongoDB update inside 2 nested arrays of objects

  20. 20

    update value inside custom nested plist

  21. 21

    how to update nested document inside array in elasticsearch

  22. 22

    Update state of array nested inside object (ReactJS)

  23. 23

    swift dictionary nested array manipulation - cannot mutate nested array inside dictionary

  24. 24

    swift dictionary nested array manipulation - cannot mutate nested array inside dictionary

  25. 25

    Check each value inside a nested array of a dictionary Swift

  26. 26

    how to split a nested dictionary inside a column of a dataframe into new rows?

  27. 27

    Populate NSDictionary in tableview from a nested plist array (inside a dictionary)

  28. 28

    matlab update two set of subplots inside a loop

  29. 29

    Unable to update nested dictionary value in multiprocessing's manager.dict()

HotTag

Archive