How do you create nested dict in Python?

PythonProgramming

You can create a nested dictionary using a nested syntax, just like you would define a JSON object.

example

a = {
   'foo': 45,
   'bar': {
      'baz': 100,
     'tru': "Hello"
   }
}

You can access the objects just like you would access a normal dict. You can also use chaining of [] operators to get the deeper levels without declaring additional variables. 

example

a = {
   'foo': 45,
   'bar': {
      'baz': 100,
      'tru': "Hello"
   }
}
print(a['bar']['baz'])

Output

This will give the output −

100
raja
Published on 19-Apr-2018 11:16:18
Advertisements