data_garden data_garden - 4 months ago 13
Python Question

Spotipy - track names and energy values

def lowEnergy():

tids = []
# get songs by recommendation
results = sp.recommendations(seed_artists = [artist['id']])

#retrieve uris for songs
for track in results['tracks']:
tids.append(track['uri'])

#get audio_features
features = sp.audio_features(tids)

#pick energies
energy = [x['energy'] for x in features]

#here I filter out low energy values
low_energy = [x for x in energy if x < 0.5]

#get track names
track_names = [sp.track(uri)['name'] for uri in tids]


Now I would like to filter out track names by energy values below 0.5, like so:

track name1 - 0.49
track name2 - 0.34


and so on.

but I'm struggling here...how do I achieve this? thanks

Answer

Not tested, but hope it works.

for i, e in enumerate(energy):
    if e < 0.5:
        track_name = sp.track(tids[i])['name']
        print("{} - {}".format(track_name, e))