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 as output
['CEO', 'CTO', 'Devops']
Expected output
{'Jhon':'CEO','Bill':'CTO','MATEW':'Devops'}
Thanks in Advance
CodePudding user response:
You want to simply take the items()
of your list then sort them using the values in roles.
Roles_dict1 = {'CEO':1,'CTO':2,'Devops':3}
User_list = {'Jhon':'CEO','MATEW':'Devops','Bill':'CTO'}
user_list_items = sorted(User_list.items(), key=lambda item: Roles_dict1[item[1]])
print(dict(user_list_items))
This will give you:
{'Jhon': 'CEO', 'Bill': 'CTO', 'MATEW': 'Devops'}
If you don't see this you might have an older version of python and in that case, you might looks at collections.OrderedDict()
or just leave things as a list of items.
CodePudding user response:
You don't need the first dict for the desired output, as it is equal to User_list
.