Chapter 5: Application of dictionaries

Dictionaries as an object structure

face Josiah Wang

In the previous use case, dictionaries are used as a database of names indexed by IDs.

Indexing names by IDs

If you did the quiz from earlier, you may have noticed another example use case of dictionaries – as an object structure.

Representing an instance of a person using a dictionary

Notice the difference between the two use cases.

In the first version, the IDs are the dictionary keys, and the person’s names are the values.

In the second version, the person’s attributes (row header) are now the keys, and the attribute values are the values of the dictionary. This second example is essentially a representation of a person.

Here is how the second use case would look like as a dict.

person1 = {"id": "00-01-30", "name": "Ali", "age": 23, 
           "occupation": "student", "nationality": "uk"}

person2 = {"id": "00-09-88", "name": "Cho", "age": 25, 
           "occupation": "software engineer", 
           "nationality": "south korea"}

This second use case is very useful if you want to represent something as an object with some attributes. This way, you get a more descriptive representation compared to, say, tuples. For example, compare point = {"x": 2, "y": 5} versus point = (2, 5), and point["x"] versus point[0]. Which do you feel is more easy to understand? (There is no exact answer - you might prefer one over another depending on the situation!)

Task: Load list of employees

Download employees_detail.txt, and again save it to the same directory as where you plan to write your script.

Create a new Python script (name it however you like).

Your task is to write a function load_employees() that reads from a given text file and returns a list, where each element in the list is a dict representation of the employee containing the employee’s id, name, age and nationality as keys. The value of age should be represented as an int, the others as strs.

The function should take one input argument: a str that specifies the name of the file from which to read.

Assume that the text file will always be in the following format (employee names and nationalities will not contain a comma).

14835634,Slađana Ellsworth,24,poland
69983058,Arianna Dragović,37,brazil
69448225,Delara Babič,32,russia
83512249,Goda MacBeth,27,india

I previously showed you some code for reading from a file:

textfile = open("employees_detail.txt")
for line in textfile:
    # Do something with line
    stripped_line = line.strip()

The actual recommended way to open a file in Python is to use the with keyword. This ensures that there are no issues with the file if your program crashes halfway (e.g. the file is not closed properly, and gets corrupted or become inaccessible). This is more important when you are writing to the file (it is less of an issue if you are only reading the file).

with open("employees_detail.txt") as textfile:
    for line in textfile:
        # Do something with line
        stripped_line = line.strip()

Sample usage

>>> employees = load_employees("employees_detail.txt")
>>> print(len(employees))
20
>>> print(employees[0])
{'id': '14835634', 'name': 'Slađana Ellsworth', 'age': 24, 'nationality': 'poland'}

Use the pprint.pp(your_complicated_data_structure) function from the pprint module (remember to import this) to “pretty print” a more readable nested list/dictionary!