this week dictionaries - not exactly webster’s using the debugger
TRANSCRIPT
This Week
• Dictionaries
- not exactly Webster’s
• Using the debugger
Dictionaries
• Generalizes the concept we are used to.• Have a key : value pair• Webster’s Dictionary
key = wordvalue = definition of word
• Use the key to locate the corresponding value
• We say the key indexes into the dictionary
• Keys are unique.
Dictionary ExamplesStudent grades file
key = student number, value = grade
GDP for Canada since 1900key = year, value = GDP
Growth rate of fruit flieskey = time, value = number of fruit flies
Dictionaries and Python
dict_name = {key1 : value1, key2 : value2, …}
Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon
emissions = {1799 : 1, 1800 : 70, 1801 : 74,
1802 : 82, 1902 : 215630, 2002 :
1733297}
Look up entry:emissions[1902] or
emissions.get(1902)
Dictionaries and Python
dict_name = {key1 : value1, key2 : value2, …}
Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon
emissions = {1799 : 1, 1800 : 70, 1801 : 74,
1802 : 82, 1902 : 215630, 2002 :
1733297}
Add entry:emissions[1950] = 734914
Dictionaries and Pythondict_name = {key1 : value1, key2 : value2, …}
Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon
emissions = {1799 : 1, 1800 : 70, 1801 : 74,
1802 : 82, 1902 : 215630, 2002 :
1733297}
Delete entry:del emissions[1902]
Dictionaries and Pythondict_name = {key1 : value1, key2 : value2, …}
Example: – N. American fossil fuel emissions by year – Units are 1000s of metric tons of carbon
emissions = {1799 : 1, 1800 : 70, 1801 : 74,
1802 : 82, 1902 : 215630, 2002 :
1733297}
Check for membership:1950 in emissions
More Dictionary Operations
Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]
Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]
Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),
(1802, 82), (1902, 215630), (2002, 1733297)]
Looping over dictionaries:for key in emissions:
print(key + “, “ + emissions[key])
More Dictionary Operations
Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]
Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]
Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),
(1802, 82), (1902, 215630), (2002, 1733297)]
Looping over dictionaries:for key in emissions.keys():
print(key + “, “ + emissions[key])
More Dictionary Operations
Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]
Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]
Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),
(1802, 82), (1902, 215630), (2002, 1733297)]
Looping over dictionaries:for value in emissions.values():
print(value)
More Dictionary Operations
Create a list of keys emissions.keys() [1799, 1800, 1801, 1802, 1902, 2002]
Create a list of values emissions.values() [1, 70, 74, 82, 215630, 1733297]
Create a list of (key, value) pairs emissions.items() [(1799, 1), (1800, 70), (1801, 74),
(1802, 82), (1902, 215630), (2002, 1733297)]
Looping over dictionaries:for (key, value) in emissions.items():
print(key + ‘\t’ + value)
More Dictionary Operationsemissions = {1799 : 1, 1800 : 70, 1801 : 74, 1802
: 82, 1902 : 215630, 2002 : 1733297}
emissions2 = {1850 : 5418, 1875 : 37561,
1925 : 486728, 1950 : 735036, 2004 : 1824421}
Add the contents of one dictionary to another:
emissions.update(emissions2)
emissions is: {1799 : 1, 1800 : 70, 1801 : 74, 1802 : 82, 1902
: 215630, 2002 : 1733297, 1925 : 486728,
1950 : 735036, 2004 : 1824421, 1850 : 5418,
1875 : 37561}