First of all, your description and example of a nested dictionary is correct. However, I am not totally clear on what the data you are showing is. In your example output from the api, the code segment ends with a comma, you should edit it so that people can see the whole output with correct formatting. As the example stands currently, it does not have valid syntax in python. If the actual api output is exactly as you've shown it, then it looks like the data is formatted in json and not python. If you want to see how parse json data in python check this answer, Deserialize a json string to an object in python .
I am going to assume that the data you get from the API looks something like this (I removed the comma and added a back brace and enclosed the set with curly braces to make it valid python, I've also assigned it to a variable so we can access it's values by key):
api_output = {
"Brands": {[
{
"brand_id": "brand1",
"brand_display": "Brand One",
"brand_is_common": "No",
"brand_country": "UK"
},
{
"brand_id": "brand2",
"brand_display": "BRand Two",
"brand_is_common": "Yes",
"brand_country": "USA"
},
{
"brand_id": "brand3",
"brand_display": "Brand Three",
"brand_is_common": "No",
"brand_country": "UK"
}]}
}
We are looking at an dictionary with the following structure:
example_dict = {
"example_key":{
[ #the square brackets denote a list
{"sub_key":sub_value,"sub_key0":sub_value0},
{"sub_key":sub_value} #these dicts are stored in the list
]
}
}
Your intuition was correct, the api is not returning a nested dictionary. The data format seems to be a dictionary with a string key, whose value is a list, which contains a set of dictionaries.
So going back to your example, we need to navigate both a list and a dictionary. To access values in a list we go by index, and to access values in a dictionary we go by key. Since the list of dictionaries is stored as a value of another dictionary we use it's corresponding key to access the list.
list_of_brands = api_output["Brands"] #assign the value of "Brands" to a variable
#we can check the value type (not needed but useful for understanding whats going on)
type(list_of_brands)
#out: list
Now we need to iterate through the list by index to get to the dictionaries it contains, and then assuming want to check every key in every dictionary, we will iterate through the keys of the dictionary and do something with the value:
for brand in list_of_brands: # for each dictionary in the list
for key in brand: # loop through the keys of the current dict,
print(" key: " + key + "\n value: " + brand[key]) #prints the key and its value
To put it all together the code looks like this:
api_output = {
"Brands": {[
{
"brand_id": "brand1",
"brand_display": "Brand One",
"brand_is_common": "No",
"brand_country": "UK"
},
{
"brand_id": "brand2",
"brand_display": "BRand Two",
"brand_is_common": "Yes",
"brand_country": "USA"
},
{
"brand_id": "brand3",
"brand_display": "Brand Three",
"brand_is_common": "No",
"brand_country": "UK"
}]}
}
for brand in list_of_brands: # for each dictionary in the list
for key in brand: # loop through the keys of the current dict,
print(" key: " + key + "\n value: " + brand[key]) #prints the key and its value
Hopefully this helped you better understand what is going on!