Posts: 5
Threads: 1
Joined: Jan 2025
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!
Posts: 1,090
Threads: 143
Joined: Jul 2017
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}}
Posts: 1,950
Threads: 8
Joined: Jun 2018
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.
Posts: 5
Threads: 1
Joined: Jan 2025
(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}}}
Posts: 5
Threads: 1
Joined: Jan 2025
Jan-02-2025, 02:18 PM
(This post was last modified: Jan-02-2025, 02:19 PM by dave0823.)
(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}}
Posts: 5
Threads: 1
Joined: Jan 2025
(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.
Posts: 6,779
Threads: 20
Joined: Feb 2020
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
Posts: 5
Threads: 1
Joined: Jan 2025
(Jan-02-2025, 03:14 PM)deanhystad Wrote: schema['keys']['2002'] = {'type': 'integer', 'default': '5678'}
Even better! Thanks
Posts: 6,779
Threads: 20
Joined: Feb 2020
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'].
|