Skip to content Skip to sidebar Skip to footer

Append To A List Within A Dictionary

I want to obtain output like {'episodes': [{'season': 1, 'plays': 0, 'episode': 11}, {'season': 2, 'plays': 0, 'episode': 1}], 'title': 'SHOWNAME1', 'imdb_id': 'tt1855924'} {'epis

Solution 1:

You need to store your matches in a dictionary instead, keyed by title. You can then find the same show again if you encounter it in your file more than once:

shows= {}

# some loop producing entriesif title not in shows:show=shows[title]= {'episodes': []}  # new show dictionaryelse:show=shows[title]# now you have `show` dictionary to work with# add episodes directly to `show['episodes']`

After collecting all your shows, use shows.values() to extract all show dictionaries as a list.

Post a Comment for "Append To A List Within A Dictionary"