'Getting data out of dictionary of lists from Spotify API request

Heyo, I need help getting the correct info out of a call to the Spotify API using Spotipy. I am relatively new to Python, so maybe I am missing something really obvious, but I have researched for a few hours and tried everything I could think of and still am not getting the results I want. I basically want to ask Spotify for a user's saved library and take out the track title and artist from the response and save them for later. Currently when I call

tracks_response = spotify.current_user_saved_tracks(limit=1, offset=0)
print(tracks_response) 

I receive something like this:

 {'href': 'https://api.spotify.com/v1/me/tracks?offset=0&limit=1', 'items': 
 [{'added_at': '2018-05-31T18:59:42Z', 'track': {'album': {'album_type': 
 'album', 'artists': [{'external_urls': {'spotify': 
 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 'href': 
 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not', 'type': 'artist', 'uri': 
 'spotify:artist:4AA8eXtzqh5ykxtafLaPOi'}], 'available_markets': [...], 
 'external_urls': {'spotify': 
 'https://open.spotify.com/album/3XaUNjcSJ6oyhoaFTqRbLb'}, 'href': 
 'https://api.spotify.com/v1/albums/3XaUNjcSJ6oyhoaFTqRbLb', 'id': 
 '3XaUNjcSJ6oyhoaFTqRbLb', 'images': [{'height': 640, 'url': 
 'https://i.scdn.co/image/3a6afacbac6870ec6025694f4f7d931fe3d0e383', 
 'width': 640}, {'height': 300, 'url': 
 'https://i.scdn.co/image/64e4b200281890ec57c239591666b31eba03e6b8', 
 'width': 300}, {'height': 64, 'url': 
 'https://i.scdn.co/image/f217a511095e82b4a34dbaa379e2d58b4f9fe9ef', 
 'width': 64}], 'name': 'Divide & Conquer (Remixes)', 'release_date': '2017- 
  06-16', 'release_date_precision': 'day', 'type': 'album', 'uri': 
 'spotify:album:3XaUNjcSJ6oyhoaFTqRbLb'}, 'artists': [{'external_urls': 
 {'spotify': 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 
 'href': 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not', 'type': 'artist', 'uri': 
 'spotify:artist:4AA8eXtzqh5ykxtafLaPOi'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/6sUbCA1obWzdoJ1FM3I9Ks'}, 'href': 
 'https://api.spotify.com/v1/artists/6sUbCA1obWzdoJ1FM3I9Ks', 'id': 
 '6sUbCA1obWzdoJ1FM3I9Ks', 'name': 'GANZ', 'type': 'artist', 'uri':  
 'spotify:artist:6sUbCA1obWzdoJ1FM3I9Ks'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/2sphpXGg1u97VSzbm5gR2P'}, 'href': 
 'https://api.spotify.com/v1/artists/2sphpXGg1u97VSzbm5gR2P', 'id': 
 '2sphpXGg1u97VSzbm5gR2P', 'name': 'JOY.', 'type': 'artist', 'uri': 
 'spotify:artist:2sphpXGg1u97VSzbm5gR2P'}, {'external_urls': {'spotify': 
 'https://open.spotify.com/artist/3C1R0cMeIfSFxpAFxCh5yQ'}, 'href': 
 'https://api.spotify.com/v1/artists/3C1R0cMeIfSFxpAFxCh5yQ', 'id': 
 '3C1R0cMeIfSFxpAFxCh5yQ', 'name': 'Slow Hours', 'type': 'artist', 'uri': 
 'spotify:artist:3C1R0cMeIfSFxpAFxCh5yQ'}], 'available_markets': [...], 
 'disc_number': 1, 'duration_ms': 272999, 'explicit': False, 'external_ids': 
 {'isrc': 'AUDCB1600994'}, 'external_urls': {'spotify': 
 'https://open.spotify.com/track/3c5Og78p3plOCBbNLg5K9L'}, 'href': 
 'https://api.spotify.com/v1/tracks/3c5Og78p3plOCBbNLg5K9L', 'id': 
 '3c5Og78p3plOCBbNLg5K9L', 'name': 'Lone (feat. JOY.) - Slow Hours Remix', 
 'popularity': 53, 'preview_url': 'https://p.scdn.co/mp3- 
 preview/cc865407083054f5be6f1adbe9eb93295e53c670? 
 cid=6bf27521c6ff4eb2bf72698c63a1d9e8', 'track_number': 2, 'type': 'track', 
 'uri': 'spotify:track:3c5Og78p3plOCBbNLg5K9L'}}], 'limit': 1, 'next': 
 'https://api.spotify.com/v1/me/tracks?offset=1&limit=1', 'offset': 0, 
 'previous': None, 'total': 1369}

I thought this was a normal dictionary, so I would easily be able to get the Artist by using normal dictionary syntax like

 print(tracks_response['items']['track']['artists']['name'])

But when I call that I receive an error reporting:

 TypeError: list indices must be integers or slices, not str

But if I change the call to:

 print(tracks_response['items'])

I get:

 [{'added_at': '2018-05-31T18:59:42Z', 'track': {'album': {'album_type': 
 'album', 'artists': [{'external_urls': {'spotify': 
 'https://open.spotify.com/artist/4AA8eXtzqh5ykxtafLaPOi'}, 'href': 
 'https://api.spotify.com/v1/artists/4AA8eXtzqh5ykxtafLaPOi', 'id': 
 '4AA8eXtzqh5ykxtafLaPOi', 'name': 'What So Not',...

Which is the same thing as above but starting at the 'items' key... It appears that the rest of the dictionary is lists with key value pairs in them, how would I get the data that I want out of them?

Any help is appreciated, Thank you.



Solution 1:[1]

Actually tracks_response['items'] returns an array. Carefully look at the reponse.You have to iterate the item list in-order to get the 'track' object. If you need to print the name of each artist from artists list, the code will be,

for item in tracks_response['items']:
    for artist in item['track']['album']['artists']:
         print(artist['name'])   

Solution 2:[2]

Try spotifyr in R. I'm fairly new to both languages and have been trying to focus on learning python, but spotifyr mod is way better and returns dataframes in just a few lines of code. Spotipy seems like a total mess to me, and requires many lines of code and loops just to be able to view the data.

For example, in spotifyr you can view all of the attributes for every song for an artist, in a dataframe with:

library(spotifyr)
library(tidyverse)
library(knitr)

client_id <- 'your_client_id_here'
client_secret <-  'your_client_secret_here'
Sys.setenv(SPOTIFY_CLIENT_ID = client_id)
Sys.setenv(SPOTIFY_CLIENT_SECRET = client_secret)
access_token <- get_spotify_access_token()

audio_features_df <- get_artist_audio_features('Artist_name_here')

Looking through the documentation it looks like you can get dataframes of everything in spotpy using spotifyr in fractions of the time. Hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sajith Herath
Solution 2 richardec