Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List comprehension on a list of dictionaries

I have a list of dictionaries like so:

[{'end': 34,
  'entity_group': 'ORG',
  'score': 0.99919325,
  'start': 0,
  'word': ' Community College Alabama'},
 {'end': 54,
  'entity_group': 'LOC',
  'score': 0.90115756,
  'start': 42,
  'word': ' Maxwell Blvd'},
 {'end': 66,
  'entity_group': 'LOC',
  'score': 0.9890175,
  'start': 56,
  'word': ' Montgomery'},
 {'end': 70,
  'entity_group': 'LOC',
  'score': 0.9988833,
  'start': 68,
  'word': ' AL'}]

I would like to extract values of word, but only the ones where 'entity_group': 'LOC'. So for the above example, that would be:

[' Maxwell Blvd', ' Montgomery', ' AL']

I have tried to do this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[[item for item in d.items()] for d in a]

… but this does not yield what I want.

>Solution :

[d['word'] for d in a if d['entity_group'] == 'LOC']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading