Yu Ni
Yu Ni

Reputation: 65

How to loop through a list of dict to get a specific value?

How can I get all the titles from below list of dict. The title field is stored in the 'data' key and in some case the data key can be an empty list.

all_movies = [{'page': '1',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'Italian Spiderman', 'Year': 2007, 'imdbID': 'tt2705436'},
                        {'Title': 'Superman, Spiderman or Batman',
                         'Year': 2011,
                         'imdbID': 'tt2084949'},
                        {'Title': 'Spiderman', 'Year': 1990, 'imdbID': 'tt0100669'},
                        {'Title': 'Spiderman', 'Year': 2010, 'imdbID': 'tt1785572'},
                        {'Title': 'Fighting, Flying and Driving: The Stunts of Spiderman 3',
                         'Year': 2007,
                         'imdbID': 'tt1132238'},
                        {'Title': 'Spiderman and Grandma', 'Year': 2009, 'imdbID': 'tt1433184'},
                        {'Title': 'The Amazing Spiderman T4 Premiere Special',
                         'Year': 2012,
                         'imdbID': 'tt2233044'},
                        {'Title': 'Amazing Spiderman Syndrome',
                         'Year': 2012,
                         'imdbID': 'tt2586634'},
                        {'Title': "Hollywood's Master Storytellers: Spiderman Live",
                         'Year': 2006,
                         'imdbID': 'tt2158533'},
                        {'Title': 'Spiderman 5', 'Year': 2008, 'imdbID': 'tt3696826'}]},
              {'page': '2',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'They Call Me Spiderman',
                         'Year': 2016,
                         'imdbID': 'tt5861236'},
                        {'Title': 'The Death of Spiderman', 'Year': 2015, 'imdbID': 'tt5921428'},
                        {'Title': 'Spiderman in Cannes', 'Year': 2016, 'imdbID': 'tt5978586'}]},
              {'page': '3', 'per_page': 10, 'total': 13, 'total_pages': 2, 'data': []}]

This is the output I need:

title = ['Amazing Spiderman Syndrome',
'Fighting, Flying and Driving: The Stunts of Spiderman 3',
"Hollywood's Master Storytellers: Spiderman Live",
'Italian Spiderman',
'Spiderman',
'Spiderman',
'Spiderman 5',
'Spiderman and Grandma',
'Superman, Spiderman or Batman',
'The Amazing Spiderman T4 Premiere Special']
all_title = []
for movie in all_movies:
    title = movie['data']
    all_title.append(title)

This gives me a list of list of dict as below but I am not able to get the Title using list index

Upvotes: 0

Views: 44

Answers (2)

WoJ
WoJ

Reputation: 29987

@vx3r's answer is simpler but for the sake of completeness - the same using comprehensions:

import itertools
import json

all_movies = [{'page': '1',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'Italian Spiderman', 'Year': 2007, 'imdbID': 'tt2705436'},
                        {'Title': 'Superman, Spiderman or Batman',
                         'Year': 2011,
                         'imdbID': 'tt2084949'},
                        {'Title': 'Spiderman', 'Year': 1990, 'imdbID': 'tt0100669'},
                        {'Title': 'Spiderman', 'Year': 2010, 'imdbID': 'tt1785572'},
                        {'Title': 'Fighting, Flying and Driving: The Stunts of Spiderman 3',
                         'Year': 2007,
                         'imdbID': 'tt1132238'},
                        {'Title': 'Spiderman and Grandma', 'Year': 2009, 'imdbID': 'tt1433184'},
                        {'Title': 'The Amazing Spiderman T4 Premiere Special',
                         'Year': 2012,
                         'imdbID': 'tt2233044'},
                        {'Title': 'Amazing Spiderman Syndrome',
                         'Year': 2012,
                         'imdbID': 'tt2586634'},
                        {'Title': "Hollywood's Master Storytellers: Spiderman Live",
                         'Year': 2006,
                         'imdbID': 'tt2158533'},
                        {'Title': 'Spiderman 5', 'Year': 2008, 'imdbID': 'tt3696826'}]},
              {'page': '2',
               'per_page': 10,
               'total': 13,
               'total_pages': 2,
               'data': [{'Title': 'They Call Me Spiderman',
                         'Year': 2016,
                         'imdbID': 'tt5861236'},
                        {'Title': 'The Death of Spiderman', 'Year': 2015, 'imdbID': 'tt5921428'},
                        {'Title': 'Spiderman in Cannes', 'Year': 2016, 'imdbID': 'tt5978586'}]},
              {'page': '3', 'per_page': 10, 'total': 13, 'total_pages': 2, 'data': []}]

# get all 'data' contents together, then flattened
datas = list(itertools.chain.from_iterable([l.get('data', []) for l in all_movies]))
# extract only titles, all of them
all_titles = [k.get('Title') for k in datas]
print(json.dumps(all_titles, indent=2))

Output:

[
  "Italian Spiderman",
  "Superman, Spiderman or Batman",
  "Spiderman",
  "Spiderman",
  "Fighting, Flying and Driving: The Stunts of Spiderman 3",
  "Spiderman and Grandma",
  "The Amazing Spiderman T4 Premiere Special",
  "Amazing Spiderman Syndrome",
  "Hollywood's Master Storytellers: Spiderman Live",
  "Spiderman 5",
  "They Call Me Spiderman",
  "The Death of Spiderman",
  "Spiderman in Cannes"
]

You have some duplicated entries. If this is not what you want, use a set instead of a list:

all_titles = {k.get('Title') for k in datas}

Upvotes: 1

vx3r
vx3r

Reputation: 295

with something like this I think

  titles = []
  for movie in all_movies:
    title_list = movie.get("data", [])
    for title in title_list:
      if title.get('Title'):
        titles.append(title.get('Title'))

  print(titles)

Consider using get with default param if key is missing

Upvotes: 2

Related Questions