I have a list of dictionaries
list_ = [
{'id': '1', 'name': 'ABC', 'email': 'abc@gmail.com', 'role': 'Tester'},
{'id': '2', 'name': 'CDE', 'email': 'cde@gmail.com', 'role': 'Developer'},
{'id': '3', 'name': 'fgh', 'email': 'fgh@gmail.com', 'role': 'Developer'},
{'id': '2', 'name': 'CDE', 'email': 'cde@gmail.com', 'role': 'Tester'},
{'id': '1', 'name': 'ABC', 'email': 'abc@gmail.com', 'role': 'Developer'}
]
I am trying to extract a particular role from the list which matches the key.
Developer has higher priority than Tester. If for a particular ID both Developer and Tester is present then the output should be Developer.
However, if the list has Tester coming first, then my code won't work, it will give Tester instead of Developer.
How can I do this correctly? Do I need to put a break statement?
def find_role(id_, list_):
role = 'user'
for each in list_:
if id_ == each['id'] and each['role'] == 'Developer':
role = 'Developer'
return role
if id_ == each['id'] and each['role'] == 'Tester':
role = 'Tester'
return role
return role
find_role('1', list_)