Chapter 5: Application of dictionaries

Querying the employee database

face Josiah Wang

Continuing on from the previous task, let us now query employee names by searching for their IDs in the database.

You can just code in the same script file as load_database() from earlier, since this is a continuation of the previous task.

Your follow-up task now is to write a function query_employees() that takes two input arguments:

  1. a dict with employee IDs as keys and employee names as values (e.g. the return value of the load_database() function above)
  2. a list which contains a list of employee IDs whose names you wish to retrieve

The function should return a list of employee names corresponding to the given list of IDs. If an ID cannot be found in the database, the employee name should be set to None.

Sample usage

>>> employee_dict = {"111": "Joe", "222": "Luca", "333": "Harry", 
...                  "444": "William"}
...
>>> queries = ["444", "111", "555"]
>>> names = query_employees(employee_dict, queries)
>>> print(names)
['William', 'Joe', None]

>>> employee_dict = load_database("employees.txt")
>>> queries = ["14817102", "12345678", "61098940"]
>>> names = query_employees(employee_dict, queries)
>>> print(names)
['Agathe Davies', None, 'Loukios Rubio']