I have nested lists and dicts returned from an API. The last three keys in each ["lexicalEntries"][0] dict is a legend for all the definitions in the associated "entries".
dd = {
"metadata": {...},
"results": [
{
"lexicalEntries": [
{
"entries": [
...definitions
],
"language": "en-us",
"lexicalCategory": {"id": "noun", "text": "Noun"},
"text": "school",
},
{
"entries": [
...more definitions
],
"language": "en-us",
"lexicalCategory": {"id": "verb", "text": "Verb"},
"text": "school",
},
],
},
{
"lexicalEntries": [
{
"entries": [
...more definitions
],
"language": "en-us",
"lexicalCategory": {"id": "noun", "text": "Noun"},
"text": "school",
},
{
"entries": [
...more definitions
],
"language": "en-us",
"lexicalCategory": {"id": "verb", "text": "Verb"},
"text": "school",
},
],
},
],
}
The code that extracts the definitions:
def gen_dict_extract(key, var):
if hasattr(var, "items"):
for k, v in var.items():
if k == key:
yield v
if isinstance(v, dict):
for result in gen_dict_extract(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in gen_dict_extract(key, d):
yield result
count = len(list(gen_dict_extract("definitions", dd)))
gendList = list(gen_dict_extract("definitions", dd))
print(f"\nResults: {count}\n")
x = 1
for i in gendList:
print(f"{x}. {i[0].capitalize()}.\n")
x += 1
It prints this:
Results: 13
1. An institution for educating children.
2. The buildings used by an institution for educating children.
3 - 11
12. A large group of fish or sea mammals.
13. (of fish or sea mammals) form a large group.
I want it to print this example output:
Results: 13
Noun
1. An institution for educating children.
2 - 9
Verb
10. Send to school; educate.
11 - 13
Here's the full API result:
{'id': 'school', 'metadata': {'operation': 'retrieve', 'provider': 'Oxford University Press', 'schema': 'RetrieveEntry'}, 'results': [{'id': 'school', 'language': 'en-us', 'lexicalEntries': [{'entries': [{'homographNumber': '100', 'senses': [{'definitions': ['an institution for educating children'], 'id': 'm_en_gbus0907270.006', 'subsenses': [{'definitions': ['the buildings used by an institution
for educating children'], 'id': 'm_en_gbus0907270.009'}, {'definitions': ['the students and staff of a school'], 'id': 'm_en_gbus0907270.010'}, {'definitions':
["a day's work at school"], 'id': 'm_en_gbus0907270.012'}]}, {'definitions': ['any institution at which instruction is given in a particular discipline'], 'id': 'm_en_gbus0907270.016', 'subsenses': [{'definitions': ['a university'], 'id': 'm_en_gbus0907270.017'}, {'definitions': ['a department or faculty of a college concerned with a particular subject of study'], 'id': 'm_en_gbus0907270.018'}]},
{'definitions': ['a group of people, particularly writers, artists, or philosophers, sharing the same or similar ideas, methods, or style'], 'id': 'm_en_gbus0907270.020', 'subsenses': [{'definitions': ['a style, approach, or method of a specified character'], 'id': 'm_en_gbus0907270.021'}]}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'noun', 'text': 'Noun'}, 'text': 'school'}, {'entries': [{'homographNumber': '101', 'senses': [{'definitions': ['send to school; educate'], 'id': 'm_en_gbus0907270.041', 'subsenses': [{'definitions': ['train or discipline (someone) in a particular skill or activity'], 'id': 'm_en_gbus0907270.047'}]}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'verb', 'text': 'Verb'},
'text': 'school'}], 'type': 'headword', 'word': 'school'}, {'id': 'school', 'language': 'en-us', 'lexicalEntries': [{'entries': [{'homographNumber': '200', 'senses': [{'definitions': ['a large group of fish or sea mammals'], 'id': 'm_en_gbus0907280.005'}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'noun', 'text': 'Noun'}, 'text': 'school'}, {'entries': [{'homographNumber': '201', 'senses':
[{'definitions': ['(of fish or sea mammals) form a large group'], 'id': 'm_en_gbus0907280.009'}]}], 'language': 'en-us', 'lexicalCategory': {'id': 'verb', 'text': 'Verb'}, 'text': 'school'}], 'type': 'headword', 'word': 'school'}], 'word':
'school'}
The association that I want to maintain is the part of speech (e.g. noun or verb). And I want to print it as shown above in the example output.
I don't know the best approach to this problem. Please help a newbie. Even if you can just direct me on how to go about solving this problem.
Ref: I used the generator from this post: Find all occurrences of a key in nested dictionaries and lists