data_garden data_garden - 1 year ago 87
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']:

#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 do I achieve this? thanks

Answer Source

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))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download