Skip to content
Advertisement

Spotipy recommendations returns a dictionary with multiple artists instead of a single track

I’m trying to get a single recommended track when passing in a genre to the recommendations() function in the Spotipy library. This is my code: recommendations = sp.recommendations(seed_genres=['pop'], limit=1)

I get back a dictionary that seemingly has multiple artists, if anyone has tips on how to extract data from a dictionary like this, please let me know as I am new to Python.

    {'tracks': [{'album': {'album_type': 'SINGLE', 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1HBjj22wzbscIZ9sEb5dyf'}, 'href': 'https://api.spotify.com/v1/artists/1HBjj22wzbscIZ9sEb5dyf', 'id': '1HBjj22wzbscIZ9sEb5dyf', 'name': 'Jonas Blue', 'type': 'artist', 'uri': 'spotify:artist:1HBjj22wzbscIZ9sEb5dyf'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/5exS0bytCYdixgv02DaCm3'}, 'href': 'https://api.spotify.com/v1/artists/5exS0bytCYdixgv02DaCm3', 'id': '5exS0bytCYdixgv02DaCm3', 'name': 'William Singe', 'type': 'artist', 'uri': 'spotify:artist:5exS0bytCYdixgv02DaCm3'}], 'available_markets': ['AD', 'AE', 'AG', 'AL', 'AM', 'AO', 'AR', 'AT', 'AU', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BN', 'BO', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CD', 'CG', 'CH', 'CI', 'CL', 'CM', 'CO', 'CR', 'CV', 'CW', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FJ', 'FM', 'FR', 'GA', 'GB', 'GD', 'GE', 'GH', 'GM', 'GN', 'GQ', 'GR', 'GT', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IQ', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MN', 'MO', 'MR', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NZ', 'OM', 'PA', 'PE', 'PG', 'PH', 'PK', 'PL', 'PS', 'PT', 'PW', 'PY', 'QA', 'RO', 'RS', 'RW', 'SA', 'SB', 'SC', 'SE', 'SG', 'SI', 'SK', 'SL', 'SM', 'SN', 'SR', 'ST', 'SV', 'SZ', 'TD', 'TG', 'TH', 'TJ', 'TL', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VC', 'VE', 'VN', 'VU', 'WS', 'XK', 'ZA', 'ZM', 'ZW'], 'external_urls': {'spotify': 'https://open.spotify.com/album/1PJTxJTsQxGT7KTY8gBfR7'}, 'href': 'https://api.spotify.com/v1/albums/1PJTxJTsQxGT7KTY8gBfR7', 'id': '1PJTxJTsQxGT7KTY8gBfR7', 'images': [{'height': 640, 'url': 'https://i.scdn.co/image/ab67616d0000b27321367d8a475851a923438320', 'width': 640}, {'height': 300, 'url': 'https://i.scdn.co/image/ab67616d00001e0221367d8a475851a923438320', 'width': 300}, {'height': 64, 'url': 'https://i.scdn.co/image/ab67616d0000485121367d8a475851a923438320', 'width': 64}], 'name': 'Mama', 'release_date': '2017-05-05', 'release_date_precision': 'day', 'total_tracks': 1, 'type': 'album', 'uri': 'spotify:album:1PJTxJTsQxGT7KTY8gBfR7'}, 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1HBjj22wzbscIZ9sEb5dyf'}, 'href': 'https://api.spotify.com/v1/artists/1HBjj22wzbscIZ9sEb5dyf', 'id': '1HBjj22wzbscIZ9sEb5dyf', 'name': 'Jonas Blue', 'type': 'artist', 'uri': 'spotify:artist:1HBjj22wzbscIZ9sEb5dyf'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/5exS0bytCYdixgv02DaCm3'}, 'href': 'https://api.spotify.com/v1/artists/5exS0bytCYdixgv02DaCm3', 'id': '5exS0bytCYdixgv02DaCm3', 'name': 'William Singe', 'type': 'artist', 'uri': 'spotify:artist:5exS0bytCYdixgv02DaCm3'}], 'available_markets': ['AD', 'AE', 'AG', 'AL', 'AM', 'AO', 'AR', 'AT', 'AU', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BN', 'BO', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CD', 'CG', 'CH', 'CI', 'CL', 'CM', 'CO', 'CR', 'CV', 'CW', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FJ', 'FM', 'FR', 'GA', 'GB', 'GD', 'GE', 'GH', 'GM', 'GN', 'GQ', 'GR', 'GT', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IQ', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MN', 'MO', 'MR', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NZ', 'OM', 'PA', 'PE', 'PG', 'PH', 'PK', 'PL', 'PS', 'PT', 'PW', 'PY', 'QA', 'RO', 'RS', 'RW', 'SA', 'SB', 'SC', 'SE', 'SG', 'SI', 'SK', 'SL', 'SM', 'SN', 'SR', 'ST', 'SV', 'SZ', 'TD', 'TG', 'TH', 'TJ', 'TL', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VC', 'VE', 'VN', 'VU', 'WS', 'XK', 'ZA', 'ZM', 'ZW'], 'disc_number': 1, 'duration_ms': 184133, 'explicit': False, 'external_ids': {'isrc': 'GBUM71701777'}, 'external_urls': {'spotify': 'https://open.spotify.com/track/47OVNnZJzIkrsEiZ4n187p'}, 'href': 'https://api.spotify.com/v1/tracks/47OVNnZJzIkrsEiZ4n187p', 'id': '47OVNnZJzIkrsEiZ4n187p', 'is_local': False, 'name': 'Mama', 'popularity': 62, 'preview_url': None, 'track_number': 1, 'type': 'track', 'uri': 'spotify:track:47OVNnZJzIkrsEiZ4n187p'}], 'seeds': [{'initialPoolSize': 1000, 'afterFilteringSize': 1000, 'afterRelinkingSize': 1000, 'id': 'pop', 'type': 'GENRE', 'href': None}]}

Thanks!

Advertisement

Answer

sp.recommendations(seed_genres=['pop'], limit=1)

returns a single track based on the given seed. Now a track can have multiple artists. You can list the names like this:

d = {'tracks': [{'album': {'album_type': 'SINGLE', 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1HBjj22wzbscIZ9sEb5dyf'}, 'href': 'https://api.spotify.com/v1/artists/1HBjj22wzbscIZ9sEb5dyf', 'id': '1HBjj22wzbscIZ9sEb5dyf', 'name': 'Jonas Blue', 'type': 'artist', 'uri': 'spotify:artist:1HBjj22wzbscIZ9sEb5dyf'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/5exS0bytCYdixgv02DaCm3'}, 'href': 'https://api.spotify.com/v1/artists/5exS0bytCYdixgv02DaCm3', 'id': '5exS0bytCYdixgv02DaCm3', 'name': 'William Singe', 'type': 'artist', 'uri': 'spotify:artist:5exS0bytCYdixgv02DaCm3'}], 'available_markets': ['AD', 'AE', 'AG', 'AL', 'AM', 'AO', 'AR', 'AT', 'AU', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BN', 'BO', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CD', 'CG', 'CH', 'CI', 'CL', 'CM', 'CO', 'CR', 'CV', 'CW', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FJ', 'FM', 'FR', 'GA', 'GB', 'GD', 'GE', 'GH', 'GM', 'GN', 'GQ', 'GR', 'GT', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IQ', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MN', 'MO', 'MR', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NZ', 'OM', 'PA', 'PE', 'PG', 'PH', 'PK', 'PL', 'PS', 'PT', 'PW', 'PY', 'QA', 'RO', 'RS', 'RW', 'SA', 'SB', 'SC', 'SE', 'SG', 'SI', 'SK', 'SL', 'SM', 'SN', 'SR', 'ST', 'SV', 'SZ', 'TD', 'TG', 'TH', 'TJ', 'TL', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VC', 'VE', 'VN', 'VU', 'WS', 'XK', 'ZA', 'ZM', 'ZW'], 'external_urls': {'spotify': 'https://open.spotify.com/album/1PJTxJTsQxGT7KTY8gBfR7'}, 'href': 'https://api.spotify.com/v1/albums/1PJTxJTsQxGT7KTY8gBfR7', 'id': '1PJTxJTsQxGT7KTY8gBfR7', 'images': [{'height': 640, 'url': 'https://i.scdn.co/image/ab67616d0000b27321367d8a475851a923438320', 'width': 640}, {'height': 300, 'url': 'https://i.scdn.co/image/ab67616d00001e0221367d8a475851a923438320', 'width': 300}, {'height': 64, 'url': 'https://i.scdn.co/image/ab67616d0000485121367d8a475851a923438320', 'width': 64}], 'name': 'Mama', 'release_date': '2017-05-05', 'release_date_precision': 'day', 'total_tracks': 1, 'type': 'album', 'uri': 'spotify:album:1PJTxJTsQxGT7KTY8gBfR7'}, 'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1HBjj22wzbscIZ9sEb5dyf'}, 'href': 'https://api.spotify.com/v1/artists/1HBjj22wzbscIZ9sEb5dyf', 'id': '1HBjj22wzbscIZ9sEb5dyf', 'name': 'Jonas Blue', 'type': 'artist', 'uri': 'spotify:artist:1HBjj22wzbscIZ9sEb5dyf'}, {'external_urls': {'spotify': 'https://open.spotify.com/artist/5exS0bytCYdixgv02DaCm3'}, 'href': 'https://api.spotify.com/v1/artists/5exS0bytCYdixgv02DaCm3', 'id': '5exS0bytCYdixgv02DaCm3', 'name': 'William Singe', 'type': 'artist', 'uri': 'spotify:artist:5exS0bytCYdixgv02DaCm3'}], 'available_markets': ['AD', 'AE', 'AG', 'AL', 'AM', 'AO', 'AR', 'AT', 'AU', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BN', 'BO', 'BR', 'BS', 'BT', 'BW', 'BY', 'BZ', 'CA', 'CD', 'CG', 'CH', 'CI', 'CL', 'CM', 'CO', 'CR', 'CV', 'CW', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FJ', 'FM', 'FR', 'GA', 'GB', 'GD', 'GE', 'GH', 'GM', 'GN', 'GQ', 'GR', 'GT', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IQ', 'IS', 'IT', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MN', 'MO', 'MR', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NE', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NZ', 'OM', 'PA', 'PE', 'PG', 'PH', 'PK', 'PL', 'PS', 'PT', 'PW', 'PY', 'QA', 'RO', 'RS', 'RW', 'SA', 'SB', 'SC', 'SE', 'SG', 'SI', 'SK', 'SL', 'SM', 'SN', 'SR', 'ST', 'SV', 'SZ', 'TD', 'TG', 'TH', 'TJ', 'TL', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VC', 'VE', 'VN', 'VU', 'WS', 'XK', 'ZA', 'ZM', 'ZW'], 'disc_number': 1, 'duration_ms': 184133, 'explicit': False, 'external_ids': {'isrc': 'GBUM71701777'}, 'external_urls': {'spotify': 'https://open.spotify.com/track/47OVNnZJzIkrsEiZ4n187p'}, 'href': 'https://api.spotify.com/v1/tracks/47OVNnZJzIkrsEiZ4n187p', 'id': '47OVNnZJzIkrsEiZ4n187p', 'is_local': False, 'name': 'Mama', 'popularity': 62, 'preview_url': None, 'track_number': 1, 'type': 'track', 'uri': 'spotify:track:47OVNnZJzIkrsEiZ4n187p'}], 'seeds': [{'initialPoolSize': 1000, 'afterFilteringSize': 1000, 'afterRelinkingSize': 1000, 'id': 'pop', 'type': 'GENRE', 'href': None}]}
for i in d["tracks"][0]["artists"]:
    print(i["name"])

Output:

Jonas Blue
William Singe
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement