Home > database >  Compare Dictionary values of another Dictionary KEYS
Compare Dictionary values of another Dictionary KEYS

Time:02-12

Roles_dict1 = {'CEO':1,'CTO':2,'Devops':3}
User_list = {'Jhon':'CEO','MATEW':'Devops','Bill':'CTO'}

for key in Roles_dict1.keys:
            Z = [b for b in Roles_dict1.keys() if any(b in a for a in User_list.values())]
print(Z)

I'm trying to print Z in the order of Roles_dict1

I'm getting this following error

File "main.py", line 15, in for key in Roles_dict1.keys:

TypeError: 'builtin_function_or_method' object is not iterable*

Expected output

{'Jhon':'CEO','Bill':'CTO','MATEW':'Devops'}

Thanks in Advance

CodePudding user response:

Should be Roles_dict1.keys() rather than Roles_dict1.keys.

CodePudding user response:

Try the following.

Roles_dict1 = {'CEO':1,'CTO':2,'Devops':3}
User_list = {'Jhon':'CEO','MATEW':'Devops','Bill':'CTO'}

print(sorted(User_list.items(), key=lambda x:Roles_dict1[x[1]]))

Result:

[('Jhon', 'CEO'), ('Bill', 'CTO'), ('MATEW', 'Devops')]

You can cast this as a dictionary by applying dict(...) to the resulting list.

  • Related