[GH-ISSUE #177] Get list of tracks in a playlist #90

Closed
opened 2026-02-27 23:20:46 +03:00 by kerem · 3 comments
Owner

Originally created by @lohriialo on GitHub (Mar 30, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/177

client_credentials_manager = SpotifyClientCredentials(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
top_tracks = sp.user_playlist('spotify', '5FJXhjdILmRA2z5bvz4nzf')

How to get each track and artist from 'top_tracks'

Originally created by @lohriialo on GitHub (Mar 30, 2017). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/177 ``` client_credentials_manager = SpotifyClientCredentials(SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET) sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) top_tracks = sp.user_playlist('spotify', '5FJXhjdILmRA2z5bvz4nzf') ``` How to get each track and artist from 'top_tracks'
kerem closed this issue 2026-02-27 23:20:47 +03:00
Author
Owner

@lohriialo commented on GitHub (Mar 30, 2017):

Here's how I got to the track names/artists but it's very dirty, I would love to clean it up

    top_tracks = sp.user_playlist('spotify', '5FJXhjdILmRA2z5bvz4nzf')
    top_tracks_data = top_tracks['tracks']
    tracks_items = top_tracks_data['items']

    song_names = []
    artist_names = []
    track_numbers = []
    for index, track in enumerate(tracks_items):
        track_names_data = track['track']
        track_name = track_names_data['name']
        song_names.append(track_name)
        track_numbers.append(index)
        print(track_name)

        artist_name_data = track_names_data['artists']
        for artist in artist_name_data:
            artist_name = artist['name']
            artist_names.append(artist_name)
            print(artist_name)

example a single track has multiple artist, hence when looping, number of artists is greater than number of tracks. Therefore, in case of a track with multiple artist, I want to concatenate the two/'three or more artist into one string, so that number of tracks = number of artist

<!-- gh-comment-id:290453529 --> @lohriialo commented on GitHub (Mar 30, 2017): Here's how I got to the track names/artists but it's very dirty, I would love to clean it up ``` top_tracks = sp.user_playlist('spotify', '5FJXhjdILmRA2z5bvz4nzf') top_tracks_data = top_tracks['tracks'] tracks_items = top_tracks_data['items'] song_names = [] artist_names = [] track_numbers = [] for index, track in enumerate(tracks_items): track_names_data = track['track'] track_name = track_names_data['name'] song_names.append(track_name) track_numbers.append(index) print(track_name) artist_name_data = track_names_data['artists'] for artist in artist_name_data: artist_name = artist['name'] artist_names.append(artist_name) print(artist_name) ``` example a single track has multiple artist, hence when looping, number of artists is greater than number of tracks. Therefore, in case of a track with multiple artist, I want to concatenate the two/'three or more artist into one string, so that number of tracks = number of artist
Author
Owner

@bentappin commented on GitHub (Mar 31, 2017):

Well, depends what you want the output to be really!

You could replace the inner loop with something like this:

artist_names.append(', '.join([a['name'] for a in track_names_data['artists']]))

<!-- gh-comment-id:290682927 --> @bentappin commented on GitHub (Mar 31, 2017): Well, depends what you want the output to be really! You could replace the inner loop with something like this: `artist_names.append(', '.join([a['name'] for a in track_names_data['artists']]))`
Author
Owner

@lohriialo commented on GitHub (Mar 31, 2017):

Exactly what I wanted, small web app running beautifully here http://spotipy.herokuapp.com/

Thanks much!!

<!-- gh-comment-id:290687930 --> @lohriialo commented on GitHub (Mar 31, 2017): Exactly what I wanted, small web app running beautifully here http://spotipy.herokuapp.com/ Thanks much!!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#90
No description provided.