Python Forum
Dictionary multi dimension support
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary multi dimension support
#1
Need Help on below error

When i write a code like below
dic1 = {}
dic1['key1']['key2']  = 'Value1'

print(dic1)
Below error is encountered
Traceback (most recent call last):
File "C:/v586150-Prsnl/learning/python/Vz450inP/kirandic.py", line 3, in <module>
dic1['key1']['key2'] = 'Value1'
KeyError: 'key1'

I am forced to define a subkey definition before assignment. Is there a better approach that Python understand the definition of the type and auto instantiate like Perl
dic1 = {}
dic1['key1'] = {}
dic1['key1']['key2']  = 'Value1'

print(dic1)
Process finished with exit code 1
Reply
#2
I am not 100% sure why you would attempt to instantiate your dictonary like you have. Normally, if I were trying to do something like that I would say:

dict = {'key1': 'Value1', 'key2': 'Value2'}
print(dict)
Output:
Output:
{'key1': 'Value1', 'key2': 'Value2'}
If I wanted a nested dictionary I would say:

dict_outer = {}

dict_outer['dict1'] = {}

dict_outer['dict1']['dict1_key'] = 'value'

print(dict_outer)
Output:

Output:
{'dict1': {'dict1_key': 'value'}}
Reply
#3
Quote:I am forced to define a subkey definition before assignment.
That is what you must do, but note that once nodes are created, you can add to them
example:
>>> dict1 = {
...     'key1': {
...         'key2': 'Value1'
...     }
... }
>>> dict1['key1']['Desc'] = 'No desc'
>>> dict1['newkey'] = {}
>>> dict1
{'key1': {'key2': 'Value1', 'Desc': 'No desc'}, 'newkey': {}}
Reply
#4
you can use dict.setdefault() method
d = {}
d.setdefault('key1', {})['key2'] = 'foo'
print(d)
d.setdefault('key1', {})['key3'] = 'bar'
print(d)
Output:
{'key1': {'key2': 'foo'}} {'key1': {'key3': 'bar', 'key2': 'foo'}}
another option is to use defaultdict from collections

from collections import defaultdict
d = defaultdict(dict)
d['key1']['key2'] = 'foo'
print(d)
d['key1']['key3'] = 'bar'
print(d)
Output:
defaultdict(<class 'dict'>, {'key1': {'key2': 'foo'}}) defaultdict(<class 'dict'>, {'key1': {'key2': 'foo', 'key3': 'bar'}})
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Array dimension don't match asja2010 0 1,016 Feb-23-2023, 04:22 PM
Last Post: asja2010
  x and y must have same first dimension, but have shapes (1,) and (50,) asja2010 5 2,478 Jan-12-2023, 07:24 PM
Last Post: deanhystad
  Strange error ValueError: dimension mismatch Anldra12 0 1,937 Aug-17-2021, 07:54 AM
Last Post: Anldra12
  ValueError: dimension mismatch Anldra12 0 3,341 Jul-17-2021, 04:46 PM
Last Post: Anldra12
  ValueError: x and y must have same first dimension, but have shapes (11,) and (15406, hobbyist 17 148,966 Mar-22-2021, 10:27 AM
Last Post: hobbyist
  Error When Plotting ValueError: x and y must have same first dimension JoeDainton123 1 9,015 Oct-04-2020, 12:58 PM
Last Post: scidam
  Wrong dimension for my plot Jemeronimo 1 1,983 Apr-25-2019, 06:19 PM
Last Post: Larz60+
  Fast method of searching a string in a multi-dimension array? draems 6 5,887 Feb-20-2017, 01:02 PM
Last Post: Ofnuts
  Build a multi-variable dictionary birdieman 4 4,969 Jan-06-2017, 04:34 PM
Last Post: birdieman

Forum Jump:

User Panel Messages

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