Flip a mapping

Mediumdictionariessetssorted· 20 XP

A recommendation job stores each user's favourite genre as {user: genre}. A marketing email needs it the other way round: for each genre, who likes it.

Write a function fans_by_genre(favourites) that returns a dictionary mapping each genre to a sorted list of users, with the genres in alphabetical order too.

How it's checked

Your code runs, then the check below runs and its printed output is compared with ours.

  • fans_by_genre({"u3": "jazz", "u1": "rock", "u2": "jazz"}) returns {"jazz": ["u2", "u3"], "rock": ["u1"]}

Hints, the later steps and the answer come with Pro.

def fans_by_genre(favourites):
    ...