Lecture 13 – Dictionaries

Data 94, Spring 2021

In [1]:
names = ['bill', 'sarah', 'cal', 'nina']
In [2]:
names[2]
Out[2]:
'cal'
In [3]:
dog = {'name': 'Junior', 
       'age': 11, 
       4: ['kibble', 'treat']}
In [4]:
dog['name']
Out[4]:
'Junior'
In [5]:
dog['age']
Out[5]:
11
In [6]:
dog[4]
Out[6]:
['kibble', 'treat']
In [7]:
dog[0]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-7-d8a707b2bab8> in <module>
----> 1 dog[0]

KeyError: 0

Quick Check 1

In [8]:
bears = {
    'polar': {
        'color': 'white',
        'weight_range': [175, 700],
        'hungry': True
    },
    'grizzly': {
        'color': 'brown',
        'weight_range': [130, 360],
        'endangered': False
    },
    None: ['koala', 'panda']
}
In [ ]:
 

Modification

In [9]:
# {} creates a new empty dictionary
slang = {}
slang
Out[9]:
{}
In [10]:
slang['btw'] = 'by the way'
slang
Out[10]:
{'btw': 'by the way'}
In [11]:
slang['nw'] = 'no worries'
slang
Out[11]:
{'btw': 'by the way', 'nw': 'no worries'}
In [12]:
slang
Out[12]:
{'btw': 'by the way', 'nw': 'no worries'}
In [13]:
slang['btw'] = 'bring the windex'
slang
Out[13]:
{'btw': 'bring the windex', 'nw': 'no worries'}
In [14]:
slang['nw'] += ", it's cool"
slang
Out[14]:
{'btw': 'bring the windex', 'nw': "no worries, it's cool"}

Quick Check 2

In [15]:
two = 1
numbers = {'1': 2}
numbers['five'] = 5
numbers[two] = numbers['1']
numbers[2] = numbers[1] + numbers['five']
In [ ]:
 

Iteration

In [16]:
more_slang = {
    'haha': 'that was not funny',
    'smh': 'shake my head',
    'lol': 'laugh out loud',
    'GOAT': 'greatest of all time'
}
In [17]:
# Number of key-value pairs
len(more_slang)
Out[17]:
4
In [18]:
# We will do this often!
list(more_slang.keys())
Out[18]:
['haha', 'smh', 'lol', 'GOAT']
In [19]:
# We will not do this often
list(more_slang.values())
Out[19]:
['that was not funny',
 'shake my head',
 'laugh out loud',
 'greatest of all time']
In [20]:
# Checks if 'smh' is a key
'smh' in more_slang
Out[20]:
True
In [21]:
# Checks if 'shake my head' is a key
# It is not – it is a value
'shake my head' in more_slang
Out[21]:
False
In [22]:
more_slang
Out[22]:
{'haha': 'that was not funny',
 'smh': 'shake my head',
 'lol': 'laugh out loud',
 'GOAT': 'greatest of all time'}
In [23]:
for abb in more_slang.keys():
    print(more_slang[abb])
that was not funny
shake my head
laugh out loud
greatest of all time
In [24]:
for abb in more_slang.keys():
    print(abb, more_slang[abb])
haha that was not funny
smh shake my head
lol laugh out loud
GOAT greatest of all time

Example: replace slang

In [25]:
# Replaces all abbreviations in text
# that are defined in more_slang
# with their full forms

def replace_slang(text):
    for abb in more_slang.keys():
        if abb in text:
            text = text.replace(abb, more_slang[abb])
    return text
In [26]:
replace_slang('smh, I did not lol')
Out[26]:
'shake my head, I did not laugh out loud'
In [27]:
replace_slang('serena is the GOAT')
Out[27]:
'serena is the greatest of all time'

Example: area codes

In [28]:
# Ignore this code, just run it.

codes_dict = {}

f = open('data/areacodes.txt', 'r')
s = f.read()

for l in s.split('\n')[:-1]:
    code, state = l.split(' — ')
    codes_dict[int(code)] = state
In [29]:
codes_dict
Out[29]:
{201: 'New Jersey',
 202: 'District of Columbia',
 203: 'Connecticut',
 205: 'Alabama',
 206: 'Washington',
 207: 'Maine',
 208: 'Idaho',
 209: 'California',
 210: 'Texas',
 212: 'New York',
 213: 'California',
 214: 'Texas',
 215: 'Pennsylvania',
 216: 'Ohio',
 217: 'Illinois',
 218: 'Minnesota',
 219: 'Indiana',
 224: 'Illinois',
 225: 'Louisiana',
 228: 'Mississippi',
 229: 'Georgia',
 231: 'Michigan',
 234: 'Ohio',
 239: 'Florida',
 240: 'Maryland',
 248: 'Michigan',
 251: 'Alabama',
 252: 'North Carolina',
 253: 'Washington',
 254: 'Texas',
 256: 'Alabama',
 260: 'Indiana',
 262: 'Wisconsin',
 267: 'Pennsylvania',
 269: 'Michigan',
 270: 'Kentucky',
 276: 'Virginia',
 278: 'Michigan',
 281: 'Texas',
 283: 'Ohio',
 301: 'Maryland',
 302: 'Delaware',
 303: 'Colorado',
 304: 'West Virginia',
 305: 'Florida',
 307: 'Wyoming',
 308: 'Nebraska',
 309: 'Illinois',
 310: 'California',
 312: 'Illinois',
 313: 'Michigan',
 314: 'Missouri',
 315: 'New York',
 316: 'Kansas',
 317: 'Indiana',
 318: 'Louisiana',
 319: 'Iowa',
 320: 'Minnesota',
 321: 'Florida',
 323: 'California',
 325: 'Texas',
 330: 'Ohio',
 331: 'Illinois',
 334: 'Alabama',
 336: 'North Carolina',
 337: 'Louisiana',
 339: 'Massachusetts',
 340: 'US Virgin Islands',
 341: 'California',
 347: 'New York',
 351: 'Massachusetts',
 352: 'Florida',
 360: 'Washington',
 361: 'Texas',
 369: 'California',
 380: 'Ohio',
 385: 'Utah',
 386: 'Florida',
 401: 'Rhode Island',
 402: 'Nebraska',
 404: 'Georgia',
 405: 'Oklahoma',
 406: 'Montana',
 407: 'Florida',
 408: 'California',
 409: 'Texas',
 410: 'Maryland',
 412: 'Pennsylvania',
 413: 'Massachusetts',
 414: 'Wisconsin',
 415: 'California',
 417: 'Missouri',
 419: 'Ohio',
 423: 'Tennessee',
 424: 'California',
 425: 'Washington',
 430: 'Texas',
 432: 'Texas',
 434: 'Virginia',
 435: 'Utah',
 440: 'Ohio',
 442: 'California',
 443: 'Maryland',
 464: 'Illinois',
 469: 'Texas',
 470: 'Georgia',
 475: 'Connecticut',
 478: 'Georgia',
 479: 'Arkansas',
 480: 'Arizona',
 484: 'Pennsylvania',
 501: 'Arkansas',
 502: 'Kentucky',
 503: 'Oregon',
 504: 'Louisiana',
 505: 'New Mexico',
 507: 'Minnesota',
 508: 'Massachusetts',
 509: 'Washington',
 510: 'California',
 512: 'Texas',
 513: 'Ohio',
 515: 'Iowa',
 516: 'New York',
 517: 'Michigan',
 518: 'New York',
 520: 'Arizona',
 530: 'California',
 539: 'Oklahoma',
 540: 'Virginia',
 541: 'Oregon',
 551: 'New Jersey',
 557: 'Missouri',
 559: 'California',
 561: 'Florida',
 562: 'California',
 563: 'Iowa',
 564: 'Washington',
 567: 'Ohio',
 570: 'Pennsylvania',
 571: 'Virginia',
 573: 'Missouri',
 574: 'Indiana',
 575: 'New Mexico',
 580: 'Oklahoma',
 585: 'New York',
 586: 'Michigan',
 601: 'Mississippi',
 602: 'Arizona',
 603: 'New Hampshire',
 605: 'South Dakota',
 606: 'Kentucky',
 607: 'New York',
 608: 'Wisconsin',
 609: 'New Jersey',
 610: 'Pennsylvania',
 612: 'Minnesota',
 614: 'Ohio',
 615: 'Tennessee',
 616: 'Michigan',
 617: 'Massachusetts',
 618: 'Illinois',
 619: 'California',
 620: 'Kansas',
 623: 'Arizona',
 626: 'California',
 627: 'California',
 628: 'California',
 630: 'Illinois',
 631: 'New York',
 636: 'Missouri',
 641: 'Iowa',
 646: 'New York',
 650: 'California',
 651: 'Minnesota',
 657: 'California',
 660: 'Missouri',
 661: 'California',
 662: 'Mississippi',
 669: 'California',
 670: 'Northern Mariana Islands',
 671: 'Guam',
 678: 'Georgia',
 679: 'Michigan',
 681: 'West Virginia',
 682: 'Texas',
 689: 'Florida',
 701: 'North Dakota',
 702: 'Nevada',
 703: 'Virginia',
 704: 'North Carolina',
 706: 'Georgia',
 707: 'California',
 708: 'Illinois',
 712: 'Iowa',
 713: 'Texas',
 714: 'California',
 715: 'Wisconsin',
 716: 'New York',
 717: 'Pennsylvania',
 718: 'New York',
 719: 'Colorado',
 720: 'Colorado',
 724: 'Pennsylvania',
 727: 'Florida',
 731: 'Tennessee',
 732: 'New Jersey',
 734: 'Michigan',
 737: 'Texas',
 740: 'Ohio',
 747: 'California',
 754: 'Florida',
 757: 'Virginia',
 760: 'California',
 762: 'Georgia',
 763: 'Minnesota',
 764: 'California',
 765: 'Indiana',
 769: 'Mississippi',
 770: 'Georgia',
 772: 'Florida',
 773: 'Illinois',
 774: 'Massachusetts',
 775: 'Nevada',
 779: 'Illinois',
 781: 'Massachusetts',
 785: 'Kansas',
 786: 'Florida',
 787: 'Puerto Rico',
 801: 'Utah',
 802: 'Vermont',
 803: 'South Carolina',
 804: 'Virginia',
 805: 'California',
 806: 'Texas',
 808: 'Hawaii',
 810: 'Michigan',
 812: 'Indiana',
 813: 'Florida',
 814: 'Pennsylvania',
 815: 'Illinois',
 816: 'Missouri',
 817: 'Texas',
 818: 'California',
 828: 'North Carolina',
 830: 'Texas',
 831: 'California',
 832: 'Texas',
 835: 'Pennsylvania',
 843: 'South Carolina',
 845: 'New York',
 847: 'Illinois',
 848: 'New Jersey',
 850: 'Florida',
 856: 'New Jersey',
 857: 'Massachusetts',
 858: 'California',
 859: 'Kentucky',
 860: 'Connecticut',
 862: 'New Jersey',
 863: 'Florida',
 864: 'South Carolina',
 865: 'Tennessee',
 870: 'Arkansas',
 872: 'Illinois',
 878: 'Pennsylvania',
 901: 'Tennessee',
 903: 'Texas',
 904: 'Florida',
 906: 'Michigan',
 907: 'Alaska',
 908: 'New Jersey',
 909: 'California',
 910: 'North Carolina',
 912: 'Georgia',
 913: 'Kansas',
 914: 'New York',
 915: 'Texas',
 916: 'California',
 917: 'New York',
 918: 'Oklahoma',
 919: 'North Carolina',
 920: 'Wisconsin',
 925: 'California',
 927: 'Florida',
 928: 'Arizona',
 931: 'Tennessee',
 935: 'California',
 936: 'Texas',
 937: 'Ohio',
 939: 'Puerto Rico',
 940: 'Texas',
 941: 'Florida',
 947: 'Michigan',
 949: 'California',
 951: 'California',
 952: 'Minnesota',
 954: 'Florida',
 956: 'Texas',
 957: 'New Mexico',
 959: 'Connecticut',
 970: 'Colorado',
 971: 'Oregon',
 972: 'Texas',
 973: 'New Jersey',
 975: 'Missouri',
 978: 'Massachusetts',
 979: 'Texas',
 980: 'North Carolina',
 984: 'North Carolina',
 985: 'Louisiana',
 989: 'Michigan'}
In [30]:
states_dict = {}
for code in codes_dict.keys():
    state = codes_dict[code]
    if state not in states_dict:
        states_dict[state] = [code]
    else:
        states_dict[state].append(code)
In [31]:
states_dict
Out[31]:
{'New Jersey': [201, 551, 609, 732, 848, 856, 862, 908, 973],
 'District of Columbia': [202],
 'Connecticut': [203, 475, 860, 959],
 'Alabama': [205, 251, 256, 334],
 'Washington': [206, 253, 360, 425, 509, 564],
 'Maine': [207],
 'Idaho': [208],
 'California': [209,
  213,
  310,
  323,
  341,
  369,
  408,
  415,
  424,
  442,
  510,
  530,
  559,
  562,
  619,
  626,
  627,
  628,
  650,
  657,
  661,
  669,
  707,
  714,
  747,
  760,
  764,
  805,
  818,
  831,
  858,
  909,
  916,
  925,
  935,
  949,
  951],
 'Texas': [210,
  214,
  254,
  281,
  325,
  361,
  409,
  430,
  432,
  469,
  512,
  682,
  713,
  737,
  806,
  817,
  830,
  832,
  903,
  915,
  936,
  940,
  956,
  972,
  979],
 'New York': [212,
  315,
  347,
  516,
  518,
  585,
  607,
  631,
  646,
  716,
  718,
  845,
  914,
  917],
 'Pennsylvania': [215, 267, 412, 484, 570, 610, 717, 724, 814, 835, 878],
 'Ohio': [216, 234, 283, 330, 380, 419, 440, 513, 567, 614, 740, 937],
 'Illinois': [217,
  224,
  309,
  312,
  331,
  464,
  618,
  630,
  708,
  773,
  779,
  815,
  847,
  872],
 'Minnesota': [218, 320, 507, 612, 651, 763, 952],
 'Indiana': [219, 260, 317, 574, 765, 812],
 'Louisiana': [225, 318, 337, 504, 985],
 'Mississippi': [228, 601, 662, 769],
 'Georgia': [229, 404, 470, 478, 678, 706, 762, 770, 912],
 'Michigan': [231,
  248,
  269,
  278,
  313,
  517,
  586,
  616,
  679,
  734,
  810,
  906,
  947,
  989],
 'Florida': [239,
  305,
  321,
  352,
  386,
  407,
  561,
  689,
  727,
  754,
  772,
  786,
  813,
  850,
  863,
  904,
  927,
  941,
  954],
 'Maryland': [240, 301, 410, 443],
 'North Carolina': [252, 336, 704, 828, 910, 919, 980, 984],
 'Wisconsin': [262, 414, 608, 715, 920],
 'Kentucky': [270, 502, 606, 859],
 'Virginia': [276, 434, 540, 571, 703, 757, 804],
 'Delaware': [302],
 'Colorado': [303, 719, 720, 970],
 'West Virginia': [304, 681],
 'Wyoming': [307],
 'Nebraska': [308, 402],
 'Missouri': [314, 417, 557, 573, 636, 660, 816, 975],
 'Kansas': [316, 620, 785, 913],
 'Iowa': [319, 515, 563, 641, 712],
 'Massachusetts': [339, 351, 413, 508, 617, 774, 781, 857, 978],
 'US Virgin Islands': [340],
 'Utah': [385, 435, 801],
 'Rhode Island': [401],
 'Oklahoma': [405, 539, 580, 918],
 'Montana': [406],
 'Tennessee': [423, 615, 731, 865, 901, 931],
 'Arkansas': [479, 501, 870],
 'Arizona': [480, 520, 602, 623, 928],
 'Oregon': [503, 541, 971],
 'New Mexico': [505, 575, 957],
 'New Hampshire': [603],
 'South Dakota': [605],
 'Northern Mariana Islands': [670],
 'Guam': [671],
 'North Dakota': [701],
 'Nevada': [702, 775],
 'Puerto Rico': [787, 939],
 'Vermont': [802],
 'South Carolina': [803, 843, 864],
 'Hawaii': [808],
 'Alaska': [907]}