Lecture 11

Harold Nelson

2/21/2018

Dictionaries

A dictionary in python is simply a lookup table implemented in a form suitable for use by a computer program. It is another kind of collection in python. Figure 4.6 below from our textbook emphasizes how a dictionary differs from other python collections we have worked with.

Figure 4.6

Figure 4.6

Basic Dictionary Syntax

Example

The dictionary below describes the relationship I have with several living things.

This table could be used to lookup the relationship to me of a “person” I mention.

The code below the dictionary definition looks up how Shin is related to me. Note that the square brackets [] are the same as those used for indexing either strings or lists.

Rel = {'Shin':'Wife', 'Alex':'Son','Damian':'Son','Sherry':'Pet','Tom':'Brother'}

print(Rel['Shin'])
## Wife

Examples of Mutability

Dictionaries are mutable.

In the following code, I show several changes to Rel.

Rel = {'Shin':'Wife', 'Alex':'Son','Damian':'Son','Sherry':'Pet','Tom':'Brother'}

# Let's look at the current dictionary.
print("Original Dictionary")
for key in Rel.keys():
    print(key,"--", Rel[key])
    
# I get divorced (Just Joking!!!)
Rel['Shin'] ="Ex-wife"

# I add a girlfriend
Rel["Miss-X"] = "Girlfriend"

# Sherry, my pet parrot, dies.
del Rel['Sherry']

# I get a dog and name him Salty
Rel['Salty'] = 'Pet'

# Let's look at the new version of Rel
print(" ")
print("Revised Dictionary")
for key in Rel.keys():
    print(key,"--", Rel[key])
## Original Dictionary
## Shin -- Wife
## Alex -- Son
## Damian -- Son
## Sherry -- Pet
## Tom -- Brother
##  
## Revised Dictionary
## Shin -- Ex-wife
## Alex -- Son
## Damian -- Son
## Tom -- Brother
## Miss-X -- Girlfriend
## Salty -- Pet

Methods for Dictionaries

The following table is in our textbook and is also available as a jpg in Moodle.

Table 4.3

Table 4.3

Exercise

Create a dictionary called Classes to to describe the classes you’re taking this term. The first item could have a key of ‘CSC 101’ and a value of ‘TTh 1:00 - 2:20’.

Include your other classes in the dictionary. If you don’t have any others, make up two fictional classes. Then use the method in today’s lecture notes to iterate over the keys and display the dictionary.

Answer

Classes = {'CSC 101':"TTh 1:00 - 2:20",
"CSC 360 A":'MWF 8:00 - 9:00',
"CSC 360 F":"W 1:30 - 4:30"}

for key in Classes.keys():
    print(key,"--",Classes[key])
## CSC 101 -- TTh 1:00 - 2:20
## CSC 360 A -- MWF 8:00 - 9:00
## CSC 360 F -- W 1:30 - 4:30

Exercise

Add a class PED 100 at 4:00 to 5:00 on MWF. Then print the revised dictionary.

Answer

Classes = {'CSC 101':"TTh 1:00 - 2:20",
"CSC 360 A":'MWF 8:00 - 9:00',
"CSC 360 F":"W 1:30 - 4:30"}

Classes["PED 100"] = "MWF 4:00 - 5:00"
for key in Classes.keys():
    print(key,"--",Classes[key])
## CSC 101 -- TTh 1:00 - 2:20
## CSC 360 A -- MWF 8:00 - 9:00
## CSC 360 F -- W 1:30 - 4:30
## PED 100 -- MWF 4:00 - 5:00

Exercise

Drop the PED class you just added and print the dictionary again.

Answer

Classes = {'CSC 101':"TTh 1:00 - 2:20",
"CSC 360 A":'MWF 8:00 - 9:00',
"CSC 360 F":"W 1:30 - 4:30"}

Classes["PED 100"] = "MWF 4:00 - 5:00"
print("After add")
for key in Classes.keys():
    print(key,"--",Classes[key])
    
del Classes["PED 100"]
print(" ")
print("After drop")
for key in Classes.keys():
    print(key,"--",Classes[key])
## After add
## CSC 101 -- TTh 1:00 - 2:20
## CSC 360 A -- MWF 8:00 - 9:00
## CSC 360 F -- W 1:30 - 4:30
## PED 100 -- MWF 4:00 - 5:00
##  
## After drop
## CSC 101 -- TTh 1:00 - 2:20
## CSC 360 A -- MWF 8:00 - 9:00
## CSC 360 F -- W 1:30 - 4:30

Exercise

Use an appropriate method to see if you’re taking MTH 161.

Answer

Classes = {'CSC 101':"TTh 1:00 - 2:20",
"CSC 360 A":'MWF 8:00 - 9:00',
"CSC 360 F":"W 1:30 - 4:30"}

print("MTH 161" in Classes)
## False

Exercise

Use the get method with the alt option to look for a class you’re not taking and print the message “Not found.”

Use the same method with a class you are taking and print this result.

Answer

Classes = {'CSC 101':"TTh 1:00 - 2:20",
"CSC 360 A":'MWF 8:00 - 9:00',
"CSC 360 F":"W 1:30 - 4:30"}

Ans = Classes.get("MTH 161","Not found")
print(Ans)

Ans = Classes.get("CSC 101","Not found")
print(Ans)
## Not found
## TTh 1:00 - 2:20