0

I am using this code to extract the genres from list of dictionaries.

dict=[{'genres': ['Thriller'], 'year': '2014'}, {'genres': ['Animation','Drama'], 'year': '2014'}]
test=[i["genres"] for i in dict]
test
[['Thriller'], ['Animation', 'Drama']]

I want to remove the inner list and create single list.

Expected output:['Thriller', 'Animation', 'Drama']
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
Alph
  • 391
  • 2
  • 7
  • 18

3 Answers3

2

You can use a nested list comprehension to do this:

>>> list_of_dicts =[{'genres': ['Thriller'], 'year': '2014'}, {'genres': ['Animation','Drama'], 'year': '2014'}]
>>> [genre for d in list_of_dicts for genre in d['genres']]
['Thriller', 'Animation', 'Drama']

In case you have possible repeats, call set over this

>>> set(genre for d in list_of_dicts for genre in d['genres'])
{'Animation', 'Drama', 'Thriller'}

Taking cue from @mgilson's answer, if you want this to work with dicts which don't have the genres key, you can do

>>> list_of_dicts =[{'genres': ['Thriller'], 'year': '2014'}, {"year": '2014'}]
>>> set(genre for d in list_of_dicts for genre in d.get('genres', []))
{'Thriller'}
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
2

I'd use itertools.

First, write a generator that yields the genres lists (one after another).

import itertools
dicts = [{'genres': ['Thriller'], 'year': '2014'}, {'genres': ['Animation','Drama'], 'year': '2014'}]
genres_nested = (dct.get('genres', ()) for dct in dicts)

This particular generator is "forgiving" -- It'll allow for dicts in the list that don't have a 'genres' field.

Then, flatten that nested structure with itertools.chain.from_iterable (you could use a nested comprehension -- but I've always found itertools.chain.from_iterable to be easier to read ...):

genres = itertools.chain.from_iterable(genres_nested)

If you need a list, you can always call list on the resulting "chain" object...

print(list(genres))

And as a side benefit, other than this last stage, everything we've done was completely lazy -- No intermediate lists wasting storage on our computers. Yeah!

mgilson
  • 300,191
  • 65
  • 633
  • 696
1

list comprehension is your friend

>>> dicts=[{'genres': ['Thriller'], 'year': '2014'}, {'genres': ['Animation','Drama'], 'year': '2014'}]
>>> [gen for d in dicts for gen in d['genres']]
['Thriller', 'Animation', 'Drama']
CauseYNot
  • 1,088
  • 7
  • 15
dragon2fly
  • 2,309
  • 19
  • 23