• The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.


  • Colab link - Open colab


  • Dictionaries

  • What are Dictionaries?


  • A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are the keys that are used to access values within a dictionary.



  • An example of a Dictionary Dict:


  • 
    
    # Create the dictionary
    
    Dict = {"key1": 1, "key2": "2", "key3": [3, 3, 3], "key4": (4, 4, 4), ('key5'): 5, (0, 1): 6}
    Dict
    
    
  • The keys can be strings:


  • 
    # Access to the value by the key
    
    Dict["key1"]
    
    
  • Keys can also be any immutable object such as a tuple:


  • 
    # Access to the value by the key
    
    Dict[(0, 1)]
    
    
  • Each key is separated from its value by a colon ":". Commas separate the items, and the whole dictionary is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this "{}".


  • 
    # Create a sample dictionary
    
    release_year_dict = {"Thriller": "1982", "Back in Black": "1980", \
                        "The Dark Side of the Moon": "1973", "The Bodyguard": "1992", \
                        "Bat Out of Hell": "1977", "Their Greatest Hits (1971-1975)": "1976", \
                        "Saturday Night Fever": "1977", "Rumours": "1977"}
    release_year_dict
    
    
  • In summary, like a list, a dictionary holds a sequence of elements. Each element is represented by a key and its corresponding value.


  • Dictionaries are created with two curly braces containing keys and values separated by a colon.


  • For every key, there can only be one single value, however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples, but values can be any data type.


  • It is helpful to visualize the dictionary as a table, as in the following image. The first column represents the keys, the second column represents the values.


  • Keys


  • You can retrieve the values based on the names:


  • 
    # Get value by keys
    
    release_year_dict['Thriller'] 
    
    
  • This corresponds to:


  • Similarly for The Bodyguard


  • 
    # Get value by key
    
    release_year_dict['The Bodyguard'] 
    
    

  • Now let you retrieve the keys of the dictionary using the method release_year_dict():


  • 
    # Get all the keys in dictionary
    
    release_year_dict.keys() 
    
    
  • You can retrieve the values using the method values():


  • 
    # Get all the values in dictionary
    
    release_year_dict.values() 
    
    
  • We can add an entry:


  • 
    # Append value with key into dictionary
    
    release_year_dict['Graduation'] = '2007'
    release_year_dict
    
    
  • We can delete an entry:


  • 
    # Delete entries by key
    
    del(release_year_dict['Thriller'])
    del(release_year_dict['Graduation'])
    release_year_dict
    
    
  • We can verify if an element is in the dictionary:


  • 
    # Verify the key is in the dictionary
    
    'The Bodyguard' in release_year_dict