Created
March 23, 2021 09:01
-
-
Save AnisahTiaraPratiwi/9ce9cd76aa851b36eb1a6152413617c9 to your computer and use it in GitHub Desktop.
The groups_per_user function receives a dictionary, which contains group names with the list of users. 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def groups_per_user(group_dictionary): | |
user_groups = {} | |
# Go through group_dictionary | |
for group, users in group_dictionary.items(): | |
# Now go through the users in the group | |
for users in group_dictionary[group]: | |
# Now add the group to the the list of | |
if users in user_groups: | |
user_groups[users].append(group) | |
else: | |
user_groups[users] = [group] | |
# groups for this user, creating the entry | |
# in the dictionary if necessary | |
return(user_groups) | |
print(groups_per_user({"local": ["admin", "userA"], | |
"public": ["admin", "userB"], | |
"administrator": ["admin"] })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment