How to get the last 4 listened tracks with pylast?

127 Views Asked by At

I'm trying to get the last 4 songs I've listened to from last_fm using pylast.

So far I have this code but os returning just one song:

def get_recents(self, max_results):
        recents = self.user.get_recent_tracks(max_results)
        for song in recents:
            return str(song.track)
1

There are 1 best solutions below

0
Jonah Bishop On BEST ANSWER

The return statement will break out of your loop immediately, meaning it will only execute once. Just return a list of songs using a list comprehension:

def get_recents(self, max_results):
    recents = self.user.get_recent_tracks(max_results)
    return [str(x) for x in recents[:4]]