Dev Helpers
← Back

Invert a one-to-many ownership map

Invert a one-to-many ownership map using Python lists and dictionaries.

Open this example in the interactive library

Source code

teams = {'ops': ['Jo', 'Lee'], 'web': ['Lee', 'Kim']}
owners = {}
for team, names in teams.items():
    for name in names:
        owners.setdefault(name, []).append(team)
print(owners)

Sample output

{'Jo': ['ops'], 'Lee': ['ops', 'web'], 'Kim': ['web']}