The groups_per_user function receives a dictionary, which contains group names with the list of users. Like the groups in Linux system. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values.
basically I'm trying to reverse the assignment of groups to users instead of users to groups
That's what I have tried so far:
def groups_per_user(group_dictionary):
user_groups = {}
groups = []
# Go through group_dictionary
for group,users in group_dictionary.items():
# Now go through the users in the group
for user in users:
# Now add the group to the list of
# groups for this user, creating the entry
# in the dictionary if necessary
groups.append(group)
user_groups[user] = group
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
How can I traverse throw the list and at the same time add the user to the group name as efficient as possible?
Please excuse my grammars, that's my fist question here. Thank you