Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dict Update
#1
I can't seem to update an existing dict structure with another dict structure to get the structure I require.

Here's what I'm doing:

        
schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})
schema_update = ({'2022': {'type': 'integer', 'default': '5678'}})
schema.update(schema_update)
Output is:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}, '2022': {'type': 'integer', 'default': '5678'}}

Expected output is:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}, '2022': {'type': 'integer', 'default': 5678}}}

I'm not able to correctly format the schema, as the output incorrectly has 2 brackets after 'default':1234}} instead of a single bracket. Also, there should be another bracket at the end.

I know I can initially create a new schema containing both dict's, but I will eventually need to loop through the schema and update multiple year dict's based on a condition.

I really would appreciate any input on this. Thanks!
Reply
#2
Is this what you mean?

d1 = {'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}}
d2 = {'2022': {'type': 'integer', 'default': 5678}}
d1.update(d2)
d1

Output:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}, '2022': {'type': 'integer', 'default': 5678}}
Reply
#3
This is nested dictionary and I appears that you want to update subdictionary in schema: schema['keys']

schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})
schema_update = ({'2022': {'type': 'integer', 'default': '5678'}})

schema['keys'].update(schema_update)

# schema is now:

{'type': 'dict',
 'keys': {'2021': {'type': 'integer', 'default': 1234}, 
          '2022': {'type': 'integer', 'default': '5678'}}}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Jan-02-2025, 01:28 AM)dave0823 Wrote: I can't seem to update an existing dict structure with another dict structure to get the structure I require.

Here's what I'm doing:

        
schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})
schema_update = ({'2022': {'type': 'integer', 'default': '5678'}})
schema.update(schema_update)
Output is:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}, '2022': {'type': 'integer', 'default': '5678'}}

Expected output is:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}, '2022': {'type': 'integer', 'default': 5678}}}

I'm not able to correctly format the schema, as the output incorrectly has 2 brackets after 'default':1234}} instead of a single bracket. Also, there should be another bracket at the end.

I know I can initially create a new schema containing both dict's, but I will eventually need to loop through the schema and update multiple year dict's based on a condition.

I really would appreciate any input on this. Thanks!

Thanks, but still not the expected output.

Your output:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}, '2022': {'type': 'integer', 'default': 5678}}
Expected output:
{'type': 'dict', 'keys': {'1979': {'type': 'integer', 'default': 1234}, '1980': {'type': 'integer', 'default': 5678}}}
Reply
#5
(Jan-02-2025, 06:23 AM)Pedroski55 Wrote: Is this what you mean?

d1 = {'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}}
d2 = {'2022': {'type': 'integer', 'default': 5678}}
d1.update(d2)
d1

Output:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}, '2022': {'type': 'integer', 'default': 5678}}
Reply
#6
(Jan-02-2025, 07:45 AM)perfringo Wrote: schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})schema_update = ({'2022': {'type': 'integer', 'default': '5678'}}) schema['keys'].update(schema_update) # schema is now: {'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234},           '2022': {'type': 'integer', 'default': '5678'}}}

Thanks Perfringo, that's the expected output! Thanks for your help.
Reply
#7
No need to use update when adding 1 key.
schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})
schema['keys']['2002'] = {'type': 'integer', 'default': '5678'}
print(schema)
Output:
{'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}, '2002': {'type': 'integer', 'default': '5678'}}}
perfringo likes this post
Reply
#8
(Jan-02-2025, 03:14 PM)deanhystad Wrote: schema['keys']['2002'] = {'type': 'integer', 'default': '5678'}

Even better! Thanks
Reply
#9
One more thing. The parenthesis in your first example don't do anything. This code:
schema = ({'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}})
schema_update = ({'2022': {'type': 'integer', 'default': '5678'}})
does the same thing as this code:
schema = {'type': 'dict', 'keys': {'2021': {'type': 'integer', 'default': 1234}}}
schema_update = {'2022': {'type': 'integer', 'default': '5678'}}
In either case, this is wrong because the structure of the dictionaries do not match.
schema.update(schema_update)
This adds schema_update to schema, when you really want to add schema_update to schema['keys'].
dave0823 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 3,853 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Sort a dict in dict cherry_cherry 4 104,278 Apr-08-2020, 12:25 PM
Last Post: perfringo
  update dict as per range of values anna 7 4,277 Sep-13-2019, 04:37 PM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020