how to modify in dictionaries?

Question:

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 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"] }))
Asked By: Yahia Naeem

||

Answers:

def groups_per_user(group_dictionary):
    user_groups = {}
    # Go through group_dictionary
    for group in group_dictionary:
        # Now go through the users in the group
        for user in group_dictionary[group]:
            try:
                user_groups[user].append(group)
            except KeyError:
                user_groups[user] = [group]
            # Now add the group to the list of
# groups for this user, creating the entry
# in the dictionary if necessary

    return(user_groups)
Answered By: alec
def groups_per_user(group_dictionary):
    user_groups = {}
    for group in group_dictionary:
        for user in group_dictionary[group]:
            if user not in user_groups:
                user_groups[user] = []
            if group not in user_groups[user]:
                user_groups[user].append(group)
    return user_groups

mylist = {"local": ["admin", "userA"],
          "public":  ["admin", "userB"],
          "administrator": ["admin"] }

print(groups_per_user(mylist))
# {'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}

Answered By: Eric Frigade

Try this:

def groups_per_user(group_dictionary):
    user_groups = {}
    for grp, users in group_dictionary.items():
        for user in users:
            if user in user_groups:
                user_groups[user].append(grp)
            else:
                user_groups[user] = [grp]

    return (user_groups)

Output of your print statement:

{'admin': ['local', 'public', 'administrator'], 'userA': ['local'], 'userB': ['public']}
Answered By: Shubham Sharma
def groups_per_user(group_dictionary):
    user_groups = {}
    for key,users in group_dictionary.items():
        for user in users:
            user_groups[user]=[]
    for key in user_groups.keys():
        for ke,value in group_dictionary.items():
            for i in value:
                if(key==i):
                    user_groups[key].append(ke)
    return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],"public"["admin","userB"],"administrator": ["admin"] }))
Answered By: Rohith P R
def groups_per_user(group_dictionary):
    user_groups = {}
    # Go through group_dictionary
    for key,value in group_dictionary.items():
        # Now go through the users in the group
        for letter in value:
            if letter in user_groups:
                user_groups[letter] += [key]             
            else :
                user_groups[letter] = [key]
            
    return(user_groups)

print(groups_per_user({"local": ["admin", "userA"],
    "public":  ["admin", "userB"],
    "administrator": ["admin"] }))
Answered By: Sapan Sidhwani
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.