pythonapidictionaryimdbpy

How do I stop generating nested tuples when I make a dictionary from a dictionary of film data?


can anyone help with a creating a dictionary from a dictionary?

I have built an API call to IMDB (using imdbpy) so I can retrieve a dictionary of movie info - including the name of the main actor of the film. This API call works fine.

But when I then try and make a second dictionary like {'actor_name': ['film names,....']} from this first dictionary, I get results where every film is in its own nested tuple and what I want is simple list of films as the value of the key.

Below is an example of the first dictionary of films and actors - which I make from my API:


# Dictionary structure is: 
# Key = Name of Film, 
# Value = imdb id of film, year of issue of film, main actor name, genre of film

films = {'The Matrix': ['0133093', 1999, ['Keanu Reeves'], ['Action', 'Sci-Fi']], 'Little Buddha': ['0107426', 1993, ['Keanu Reeves'], ['Drama']], 'The First Wives Club': ['0116313', 1996, ['Goldie Hawn'], ['Comedy']], 'Replicas': ['4154916', 2018, ['Keanu Reeves'], ['Drama', 'Sci-Fi', 'Thriller']], 'Siberia': ['6494418', 2018, ['Keanu Reeves'], ['Crime', 'Romance', 'Thriller']], 'Death Becomes Her': ['0104070', 1992, ['Meryl Streep'], ['Comedy', 'Fantasy', 'Horror']], 'Godzilla vs. Kong': ['5034838', 2021, ['Alexander Skarsgård'], ['Action', 'Sci-Fi', 'Thriller']], 'The Da Vinci Code': ['0382625', 2006, ['Tom Hanks'], ['Mystery', 'Thriller']], 'Overboard': ['0093693', 1987, ['Goldie Hawn'], ['Comedy', 'Romance']], 'The Big Blue': ['0095250', 1988, ['Rosanna Arquette'], ['Adventure', 'Drama', 'Sport']]}

This is the code I use to make the second dictionary of action names as key and film names as values:

cast_names = {}

for k, v in films.items():
    film = k
    for i in v[2]:
        key = i
        if key in cast_names:
            cast_names[key] = (cast_names[key], k)
        else:
            cast_names[key] = k


print(cast_names)

And this is what I get back when I try to build a dictionary of {'actor_name' : ['film names'...]} using the code:

cast_names = {'Keanu Reeves': ((('The Matrix', 'Little Buddha'), 'Replicas'), 'Siberia'), 'Goldie Hawn': ('The First Wives Club', 'Overboard'), 'Meryl Streep': 'Death Becomes Her', 'Alexander Skarsgård': 'Godzilla vs. Kong', 'Tom Hanks': 'The Da Vinci Code', 'Rosanna Arquette': 'The Big Blue'}

It looks like each film is in a nested tuple. What I want is :

{'Keanu Reeves': ['The Matrix', 'Little Buddha', 'Replicas', 'Siberia'], 'Goldie Hawn':[.........] etc

Any suggestions?

Thanks


Solution

  • Simply create a list using [] and then .append()

    
    for k, v in films.items():
        film = k
        for i in v[2]:
            key = i
            if key in cast_names:
                cast_names[key].append(k)
            else:
                cast_names[key] = [k]
    print(cast_names)
    

    Output:

    {'Keanu Reeves': ['The Matrix', 'Little Buddha', 'Replicas', 'Siberia'], 'Goldie Hawn': ['The First Wives Club', 'Overboard'], 'Meryl Streep': ['Death Becomes Her'], 'Alexander Skarsgård': ['Godzilla vs. Kong'], 'Tom Hanks': ['The Da Vinci Code'], 'Rosanna Arquette': ['The Big Blue']}