Dictionaries 
Recall our plan: Go through common data types and
Learn how to create them 
Consider commonly used functions and methods 
See control flow and other tricks along the way 
 
This topic, compound objects: - Dictionaries 
Dictionaries are data objects that have key-value pairs associated with it
keys must be unique 
keys can be any immutable  object (containing no mutable elements) 
 
Dictionaries are a flexible data type that is
Note: These types of webpages are built from Jupyter notebooks (.ipynb files). You can access your own versions of them by clicking here . It is highly recommended that you go through and run the notebooks yourself, modifying and rerunning things where you’d like! 
Creating a Dictionary 
Creating a dictionary using dict() or {} 
Use dict(supply_key_value_pairs) or {supply_key_value_pairs} 
 
#empty dictionary =  {"key1" : [12 , - 10 , "value1" ],  #key is specified first (must be immutable) "key2" : [11 , "value2" ],       #value is then given after a : (can be anything, doesn't need to match across keys) "key3" : "value3" 
{'key1': [12, -10, 'value1'], 'key2': [11, 'value2'], 'key3': 'value3'} 
 
=  dict ([1 , ['hee' , 'haw' ]),        #passing key value pairs as a tuple 2 , 'fa' )
{1: ['hee', 'haw'], 2: 'fa'} 
 
 
Creating a dictionary using lists 
You can create a dictionary using two lists and the zip() function 
 
=  [x for  x in  "abcdefgh" ]=  [y for  y in  range (0 ,8 )]print (keys)print (values)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
[0, 1, 2, 3, 4, 5, 6, 7] 
 
{'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7} 
 
 
Creating a dictionary with dictionary comprehensions 
Can create a dictionary using dictionary comprehensions! 
Smilar to list comprehensions but we use { instead of [ 
 
=  {i: i for  i in  range (0 , 6 )}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5} 
 
=  {"abcdef" [i]: i** 2  for  i in  range (0 , 6 )}
{'a': 0, 'b': 1, 'c': 4, 'd': 9, 'e': 16, 'f': 25} 
 
 
 
Dictionary Operations 
Indexing a Dictionary 
Index with a [key] (remember unordered!) 
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ]"North" ]
['Steelers', 'Browns', 'Ravens', 'Bengals'] 
 
You can access the returned object in the same line of code 
 
We can add new key/value pairs by simply referencing a key that doesn’t exist 
Here we add the “South” key with a silly value 
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ]"South" ] =  [1 , 2 ]
{'North': ['Steelers', 'Browns', 'Ravens', 'Bengals'],
 'East': ['Patriots', 'Jets', 'Dolphins', 'Bills'],
 'West': ['Raiders', 'Chiefs', 'Chargers', 'Broncos'],
 'South': [1, 2]} 
 
You can iterate over the keys in a dictionary 
 
for  key in  AFCDivisions:              #keys are what are iterated over print (key, ' : ' , AFCDivisions[key]) #we access the value associated with the key 
North  :  ['Steelers', 'Browns', 'Ravens', 'Bengals']
East  :  ['Patriots', 'Jets', 'Dolphins', 'Bills']
West  :  ['Raiders', 'Chiefs', 'Chargers', 'Broncos']
South  :  [1, 2] 
 
You can overwrite the values similar to how you can add a key/value pair after the fact. Here we overwrite the “South” value. 
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ]"South" ] =  [1 , 2 ]"South" ] =  ["Texans" , "Colts" , "Jaguars" , "Titans" ]for  key in  AFCDivisions:print (key, ' : ' , AFCDivisions[key])
North  :  ['Steelers', 'Browns', 'Ravens', 'Bengals']
East  :  ['Patriots', 'Jets', 'Dolphins', 'Bills']
West  :  ['Raiders', 'Chiefs', 'Chargers', 'Broncos']
South  :  ['Texans', 'Colts', 'Jaguars', 'Titans'] 
 
 
Dictionary Packing & Unpacking 
We can pack  dictionaries using ** similar to how we packed a list! 
Here we create a dictionary called Divisions where we pack two dictionaries inside it 
 
=  {"AFCNorth" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"AFCEast"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"AFCWest"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ],"AFCSouth" : ["Texans" , "Colts" , "Jaguars" , "Titans" ]=  {"NFCNorth"  : ["Lions" , "Bears" , "Packers" , "Vikings" ],"NFCEast"   : ["Giants" , "Cowboys" , "Eagles" , "Admirals" ]=  {** AFCDivisions, ** NFCDivisions}for  key in  Divisions:print (key, ' : ' , Divisions[key])
AFCNorth  :  ['Steelers', 'Browns', 'Ravens', 'Bengals']
AFCEast  :  ['Patriots', 'Jets', 'Dolphins', 'Bills']
AFCWest  :  ['Raiders', 'Chiefs', 'Chargers', 'Broncos']
AFCSouth  :  ['Texans', 'Colts', 'Jaguars', 'Titans']
NFCNorth  :  ['Lions', 'Bears', 'Packers', 'Vikings']
NFCEast  :  ['Giants', 'Cowboys', 'Eagles', 'Admirals'] 
 
 
 
Dictionary Methods 
Many useful methods 
We saw how to index with [] similar to lists/tuples 
We can index with .get() instead
Advantage is that it doesn’t throw an error if the key doesn’t exist 
  
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ],"South" : ["Texans" , "Colts" , "Jaguars" , "Titans" ]"South" )
['Texans', 'Colts', 'Jaguars', 'Titans'] 
 
"Northeast" ) #doesn't throw an error  
"Northeast" ] #throws an error 
--------------------------------------------------------------------------- 
KeyError                                   Traceback (most recent call last)
<ipython-input-15-4c30b3284c03>  in <cell line: 1> () 
----> 1   AFCDivisions[ "Northeast" ]  #throws an error 
KeyError : 'Northeast' 
 
 
Return keys with .keys(); values with .values() 
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ],"South" : ["Texans" , "Colts" , "Jaguars" , "Titans" ]
dict_keys(['North', 'East', 'West', 'South']) 
 
dict_values([['Steelers', 'Browns', 'Ravens', 'Bengals'], ['Patriots', 'Jets', 'Dolphins', 'Bills'], ['Raiders', 'Chiefs', 'Chargers', 'Broncos'], ['Texans', 'Colts', 'Jaguars', 'Titans']]) 
 
Return and remove a specified key with .pop() (similar to the list method) 
 
=  {"North" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"East"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"West"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ],"South" : ["Texans" , "Colts" , "Jaguars" , "Titans" ]"North" ) #modifies the dictionary 
['Steelers', 'Browns', 'Ravens', 'Bengals'] 
 
for  key in  AFCDivisions:print (key, ' : ' , AFCDivisions[key]) #North no longer exists! 
East  :  ['Patriots', 'Jets', 'Dolphins', 'Bills']
West  :  ['Raiders', 'Chiefs', 'Chargers', 'Broncos']
South  :  ['Texans', 'Colts', 'Jaguars', 'Titans'] 
 
Merge in another dictionary with .update() 
Very similar to dictionary packing done above but it modifies one of the dictionaries 
 
=  {"AFCNorth" : ["Steelers" , "Browns" , "Ravens" , "Bengals" ],"AFCEast"  : ["Patriots" , "Jets" , "Dolphins" , "Bills" ],"AFCWest"  : ["Raiders" , "Chiefs" , "Chargers" , "Broncos" ],"AFCSouth" : ["Texans" , "Colts" , "Jaguars" , "Titans" ]=  {"NFCNorth" : ["Lions" , "Packers" , "Bears" , "Vikings" ]#combine the dictionaries for  key in  Divisions.keys():print (key, " : " , Divisions[key])
AFCNorth  :  ['Steelers', 'Browns', 'Ravens', 'Bengals']
AFCEast  :  ['Patriots', 'Jets', 'Dolphins', 'Bills']
AFCWest  :  ['Raiders', 'Chiefs', 'Chargers', 'Broncos']
AFCSouth  :  ['Texans', 'Colts', 'Jaguars', 'Titans']
NFCNorth  :  ['Lions', 'Packers', 'Bears', 'Vikings'] 
 
 
 
Video Demo 
This quick video demo shows an example of creating dictionaries with lists as the values, creating with tuples as the keys, and checking if some values occur in the dictionary using in and/or not in. Remember to pop the video out into the full player.
The notebook written in the video is available here .
from  IPython.display import  IFrame= "https://ncsu.hosted.panopto.com/Panopto/Pages/Embed.aspx?id=b270f285-6973-4e6b-aeae-b0f800f38b96&autoplay=false&offerviewer=true&showtitle=true&showbrand=true&captions=false&interactivity=all" , height= "405" , width= "720" )
        
        
 
 
Recap 
If you are on the course website, use the table of contents on the left or the arrows at the bottom of this page to navigate to the next learning material!
If you are on Google Colab, head back to our course website for our next lesson !