Dictionaries#

Note: You can explore the associated workbook for this chapter in the cloud.

In this lesson, we’re going to learn about Python dictionaries by drawing on Anelise Shrout’s Bellevue Almshouse Dataset, excerpted below.

Preview The Bellevue Almshouse Dataset

date_in first_name last_name age disease profession gender children
0 1847-04-17 Mary Gallagher 28.0 recent emigrant married f Child Alana 10 days
1 1847-04-08 John Sanin (?) 19.0 recent emigrant laborer m Catherine 2 mo
2 1847-04-17 Anthony Clark 60.0 recent emigrant laborer m Charles Riley afed 10 days
3 1847-04-08 Lawrence Feeney 32.0 recent emigrant laborer m Child
4 1847-04-13 Henry Joyce 21.0 recent emigrant NaN m Child 1 mo
5 1847-04-14 Bridget Hart 20.0 recent emigrant spinster f Child
6 1847-04-14 Mary Green 40.0 recent emigrant spinster f And child 2 months
7 1847-04-19 Daniel Loftus 27.0 destitution laborer m NaN
8 1847-04-10 James Day 35.0 recent emigrant laborer m NaN
9 1847-04-10 Margaret Farrell 30.0 recent emigrant widow f NaN
10 1847-04-10 Bridget Day 30.0 recent emigrant married f NaN
11 1847-04-10 Anthony Day 0.5 recent emigrant NaN m NaN
12 1847-04-07 James Collins 22.0 recent emigrant laborer m NaN
13 1847-04-07 Thomas Collins 21.0 recent emigrant laborer m NaN
14 1847-04-07 Pat Whalen 25.0 recent emigrant laborer m NaN
15 1847-04-17 Dan Delany 10.0 typhus NaN m NaN
16 1847-04-09 Catherine O'Harra 23.0 recent emigrant married f NaN
17 1847-04-09 Damiel O'Harra 25.0 recent emigrant laborer m NaN
18 1847-04-12 Margaret Delaney 26.0 recent emigrant married f NaN
19 1847-04-12 Michael Delany 3.0 recent emigrant NaN m NaN

We’re using the Bellevue Almshouse Dataset to practice dictionaries because we want to think deeply about the consequences of reducing human life to data even at this early stage in our Python journey. This immigration data, as Shrout argues in her essay “(Re)Humanizing Data: Digitally Navigating the Bellevue Almshouse,” was “produced with the express purpose of reducing people to bodies; bodies to easily quantifiable aspects; and assigning value to those aspects which proved that the marginalized people to who they belonged were worth less than their elite counterparts.”


Dictionary#

When we used lists with the Bellevue Almshouse data, it was easier than individually assigning individual variables. We could put multiple names into a single list and multiple ages in a single list.

By using a Python data collection type called a dictionary, we can go even further and group each person’s name, age, and profession into a single collection.

Indivudal Variables

person1_name = 'Mary Gallagher'
person2_name = 'John Sanin (?)'
person1_age = 18
person2_age = 19

Lists

names = ['Mary Gallagher', 'John Sanin(?)', 'Anthony Clark', 'Margaret Farrell']
ages = [28, 19, 60, 30]
professions = ['married', 'laborer', 'laborer', 'widow']

Dictionary

person1 = {"name": "Mary Gallagher",
             "age": 28,
             "profession": "married"}
type(person1)
dict
person2 = {"name": "John Sanin(?)",
             "age": 19,
             "profession": "laborer"}

Key-Value#

A dictionary is made up of “key”-“value” pairs, which are separated by a colon : and separated from other key-value pairs by a comma ,. A dictionary is always enclosed by curly brackets {}.

person1 = {"name": "Mary Gallagher",
             "age": 28,
             "profession": "married"}

You can check all the keys in a dictionary by using the .keys() method or all the values in a dictionary by using the .values() method.

person1.keys()
dict_keys(['name', 'age', 'profession'])
person1.values()
dict_values(['Mary Gallagher', 28, 'married'])

Access Items#

You can access a value in a dictionary by using square brackets [] and its key name (kind of like how we indexed a string or a list).

person1["name"]
'Mary Gallagher'
person1["age"]
28
person1["profession"]
'married'

Change Item#

You can change a value in a dictionary by re-assigning a new value to a dictionary key.

person1["age"] = 100
person1
{'name': 'Mary Gallagher', 'age': 100, 'profession': 'married'}
person1['profession'] = 'spinster'
person1
{'name': 'Mary Gallagher', 'age': 100, 'profession': 'spinster'}

Nested Dictionary#

You can also nest a dictionary inside another dictionary.

bellevue_people = {
                "person1":
                  {"name": "Mary Gallagher",
                   "age": 28,
                   "profession": "married"},
                "person2":
                  {"name": "John Sanin(?)",
                   "age": 19,
                   "profession": "laborer"}
                }
bellevue_people['person1']
{'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'}
bellevue_people['person1']['name']
Hide code cell output
'Mary Gallagher'
bellevue_people['person2']
{'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'}
bellevue_people['person2']['age']
19

Iterate Through Dictionary#

for person in bellevue_people.keys():
    print(person)
person_1
person_2
for person in bellevue_people.values():
    print(person)
{'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'}
{'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'}
for person in bellevue_people.values():
    if person['age'] > 20:
        name = person['name']
        age = person['age']
        print(f'{name} is more than 20 years old. She is {age}.')
Mary Gallagher is more than 20 years old. She is 28.
for person in bellevue_people.items():
    print(person)
('person_1', {'name': 'Mary Gallagher', 'age': 28, 'profession': 'married'})
('person_2', {'name': 'John Sanin(?)', 'age': 19, 'profession': 'laborer'})