In: Computer Science
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.
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for ___:
# Now go through the users in the
group
for ___:
# Now add the
group to the the list of
# 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"] }))
Code:
Output:
Raw Code:
def groups_per_user(group_dictionary):
user_groups={}
for key in group_dictionary.keys(): #loop through the keys using
keys function
for val in group_dictionary[key]: #loop through the list of values
associated with the key in dictionary
if val in user_groups.keys(): #if it is already present
user_groups[val].append(key) #appending the key as value to
user_groups
else: #if it is not present
user_groups[val]=[key] #adding val as key to user_groups and key of
group_dictionary as value list
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],"public":
["admin", "userB"],"administrator": ["admin"]
}))