How do you create nested dict in Python?



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements