Skip to content
Advertisement

How to find the values of a key in a nested list of dictionaries in Python

This is my data structure. I have 3000 instances of ‘product’ within the ‘clothes list’. Each contains another list of dictionaries that stores the reviews.

JavaScript

I am attempting to iterate through all the reviews in the dictionary and return all the reviews written by a particular username.

My Two attempts below output the same error.

JavaScript
JavaScript

The Error

TypeError: list indices must be integers or slices, not str

Target Output

JavaScript

Advertisement

Answer

Your logic review["Reviews"]["User"] can’t be iterable because Reviews is a list of dictionaries. You can’t directly call dictionary without putting any index of list.

JavaScript

Output

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