pythonpylast

How to get the last 4 listened tracks with pylast?


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)

Solution

  • 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]]