Skip to content
Advertisement

Accessing nested dictionary values in python

I am new to Python dictionaries, i’m trying to extract the numbers as positions inside the dictionary value denotations, sub category ‘span’. The dictionary looks like this:

JavaScript

I’m only interested in the denotations category, more so the numbers held inside span. I can only manage to extract the denotation information print(z["denotations"]) and I’m a bit stuck on how to go further into the dictionary, example:

Is it possible to extract the span information:

JavaScript

or even store just the numbers as positions?

JavaScript

Advertisement

Answer

The trick is to recognise that z['denotations'] is a list not a dictionary. Therefore, you need to iterate over this list to access each dictionary containing the spans.

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement