Home > Software engineering >  How to return the values of "movie"=["spiderman","endgame","justi
How to return the values of "movie"=["spiderman","endgame","justi

Time:10-06

Funday={"entertainment": [{ "game": "golf", "movies_name": [{ "movie": "Spiderman", "production": "marvel"}, { "movie": "endgame", "production": "marvel"}, { "movie": "justiceleague", "production": "DC" }, { "movie": "batman", "production": "DC"}]}]}

CodePudding user response:

movie = ["spiderman", "endgame", "justiceleague", "batman"]

for val in movie:
 print(val)

CodePudding user response:

What I believe you're trying to do is something like the following:

Funday={"entertainment": [{ "game": "golf", "movies_name": [{ "movie": "Spiderman", "production": "marvel"}, { "movie": "endgame", "production": "marvel"}, { "movie": "justiceleague", "production": "DC" }, { "movie": "batman", "production": "DC"}]}]}

movies = []

for entertainment in Funday["entertainment"]:
  for movie in entertainment["movies_name"]:
    movies.append(movie["movie"])

In the first line, I initialize the list of movies. In the second, I loop through each of the entertainment values (of which there are only one, but I'm assuming you have multiple). After that, I loop through each of the movies, and add them to movies.

However, your motive isn't really clear here. First of all, why is entertainment a list? If there's multiple games, then you might want to store movies inside a dictionary for each entertainment value:

movies = {}

for entertainment in Funday["entertainment"]:
 for movie in entertainment["movies_name"]:
   game = entertainment["game"]
     if game not in movies:
       movies[game] = []
     movies[game].append(movie["movie"])

CodePudding user response:

lst=[]

for i  in dct["entertainment"]:
     if(i['movies_name']):
        for j in i['movies_name']:
          lst.append(j['movie'])
        
print(lst)
  • Related