Editor

Mutual Follows

#45 · Graphs · Beginner · 40 XP · about 8 min

On a photo app, following goes one way: Ana can follow Ben without Ben following her back. The dictionary follows maps each user to the list of users they follow. Write mutual_follows(follows) that finds every pair of users who follow each other.

Return a list of tuples (a, b), where a comes before b alphabetically, with the whole list sorted. Some users appear only inside other users' lists, which means they follow nobody.

Examples

mutual_follows({'ana': ['ben', 'cy'], 'ben': ['ana'], 'cy': ['ben']})
# returns [('ana', 'ben')]

mutual_follows({'kim': ['lee'], 'lee': ['kim', 'max'], 'max': ['lee', 'kim']})
# returns [('kim', 'lee'), ('lee', 'max')]

In the first example Cy follows Ben, but Ben does not follow Cy back, so Ana and Ben are the only mutual pair.

Note

  • Nobody follows themselves, and no list repeats a name.
  • If there are no mutual pairs, return an empty list.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.