Python - Checking for membership inside nested dict
- by victorhooi
heya,
This is a followup questions to this one:
http://stackoverflow.com/questions/2901422/python-dictreader-skipping-rows-with-missing-columns
Turns out I was being silly, and using the wrong ID field.
I'm using Python 3.x here.
I have a dict of employees, indexed by a string, "directory_id". Each value is a nested dict with employee attributes (phone number, surname etc.). One of these values is a secondary ID, say "internal_id", and another is their manager, call it "manager_internal_id". The "internal_id" field is non-mandatory, and not every employee has one.
(I've simplified the fields a little, both to make it easier to read, and also for privacy/compliance reasons).
The issue here is that we index (key) each employee by their directory_id, but when we lookup their manager, we need to find managers by their "internal_id".
Before, when employee.keys() was a list of internal_ids, I was using a membership check on this. Now, the last part of my if statement won't work, since the internal_ids is part of the dict values, instead of the key itself.
def lookup_supervisor(manager_internal_id, employees):
if manager_internal_idis not None and manager_internal_id!= "" and manager_internal_id in employees.keys():
return (employees[manager_internal_id]['mail'], employees[manager_internal_id]['givenName'], employees[manager_internal_id]['sn'])
else:
return ('Supervisor Not Found', 'Supervisor Not Found', 'Supervisor Not Found')
So the first question is, how do I check whether the manager_internal_id is present in the dict's values.
I've tried substituting employee.keys() with employee.values(), that didn't work. Also, I'm hoping for something a little more efficient, not sure if there's a way to get a subset of the values, specifically, all the entries for employees[directory_id]['internal_id'].
Hopefully there's some Pythonic way of doing this, without using a massive heap of nested for/if loops.
My second question is, how do I then cleanly return the required employee attributes (mail, givenname, surname etc.). My for loop is iterating over each employee, and calling lookup_supervisor. I'm feeling a bit stupid/stumped here.
def tidy_data(employees):
for directory_id, data in employees.items():
# We really shouldnt' be passing employees back and forth like this - hmm, classes?
data['SupervisorEmail'], data['SupervisorFirstName'], data['SupervisorSurname'] = lookup_supervisor(data['manager_internal_id'], employees)
Thanks in advance =),
Victor