简体   繁体   中英

Using enumerate for a list of lists (json)

I'm looking at Spotify music with python. Haven't been able to find an answer online so far. I'd like to get information from each song retrieved from the API (2000 songs). I've got the artist of every song, which is a long string of json seen below:

print(artist):

#This is the output:

[[{'track': {'album': {'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1uiEZYehlNivdK3iQyAbye'},
       'href': 'https://api.spotify.com/v1/artists/1uiEZYehlNivdK3iQyAbye',
       'id': '1uiEZYehlNivdK3iQyAbye',
       'name': 'Tom Misch',
       'type': 'artist',
       'uri': 'spotify:artist:1uiEZYehlNivdK3iQyAbye'},
      {'external_urls': {'spotify': 'https://open.spotify.com/artist/2rspptKP0lPBdlJJAJHqht'},
       'href': 'https://api.spotify.com/v1/artists/2rspptKP0lPBdlJJAJHqht',
       'id': '2rspptKP0lPBdlJJAJHqht',
       'name': 'Yussef Dayes',
       'type': 'artist',
       'uri': 'spotify:artist:2rspptKP0lPBdlJJAJHqht'}]}}}, 

# and so on

Since the artist variable is a list of lists & dictionaries, when I print the length of artist I get 20:

len(artist)
20

However, within each element of the artist variable, there are 100 items:

len(artist[0])
100

I want to loop through all 2000 items in artist , and add them to one list. So far my code has been very clunky:

artist_list = []
i=0
while i<100:

        artist_list.append(artist[0][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[1][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[2][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[3][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[4][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[5][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[6][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[7][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[8][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[9][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[10][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[11][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[12][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[13][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[14][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[15][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[16][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[17][i]["track"]["album"]["artists"][0]["name"])

        i+=1

What's the best way to shorten this? I've tried using enumerate but couldn't quite figure how to do it. Any help would be much appreciated!

You can achieve this using a double for loop:

artist_list = []
for a in artist:
    for b in a:
        artist_list.append(b["track"]["album"]["artists"][0]["name"])

However, it's more pythonic (and efficient) to use a python list comprehension:

artist_list = [b["track"]["album"]["artists"][0]["name"] for a in artist for b in a]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM