Skip to content
Advertisement

Iterating through nested dictionaries and find the keywords in the value of dictionary in python

I have data in below format.

JavaScript

Here are few things I want to do with my data in python.

First to iterate over my dictionary and to find and count the keywords mentioned above in the value of “Text” both in “1” and “2” and last thing is to update the current dictionary with keywords counts (no of times keywords mentioned in “1” and “2” like below.

JavaScript

If anyone can help me, would be thankful.

Advertisement

Answer

You could use collections.Counter:

JavaScript

Output:

JavaScript

Note: Using |= to merge dicts is a Python 3.10 feature. Should not be hard to google how to do it if you are using an older version.

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