Remove inner list in list of lists

Question:

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']
Asked By: Alph

||

Answers:

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'}
Answered By: Anshul Goyal

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!

Answered By: mgilson

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']
Answered By: dragon2fly
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.