[Tutor] Square Brackets

P L la_spirou at hotmail.com
Tue Sep 29 11:19:32 EDT 2020


Hello,

Quick beginner question. I've completed the problem successfully however I couldn't figure out the following:

if I remove the square brackets from groups, it affects the else statement and I don't know why. You get an attribute error message. I figure the error message should be under the if statement and not the else statement.

Thanks for your time!
Pat

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

return(user_groups)

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


More information about the Tutor mailing list