Python Dictionary and Dictionary Manipulation Video Before starting, I should mention that the code in this blog post and in the above is available on my . video github Define a Dictionary Dictionaries are written within curly brackets {}. Define a dictionary. Keys are in red. Values are in blue. webstersDict = {'person': 'a human being','marathon': 'a running race that is about 26 miles','resist': 'to remain strong against the force','run': 'to move with haste; act quickly'} # Define a dictionary code Dictionaries are unordered data structures that map unique keys to values. The webstersDict used strings as keys in the dictionary, but dictionary keys can be any immutable data type (numbers, strings, tuples etc). Dictionary values can be just about anything (int, lists, functions, strings, etc). dictionary For example, the dictionary below, genderDict has ints as keys and strings as values. genderDict = {0: 'male',1: 'female'} # Define a dictionary An important point to emphasize is that if you try to make a key a mutable datatype (like a list), you will get an error. # Failure to define a dictionarywebstersDict = {(1, 2.0): 'tuples can be keys',1: 'ints can be keys','run': 'strings can be keys',['sock', 1, 2.0]: 'lists can NOT be keys'} Failure to define a dictionary with a list as a key. Lists are NOT immutable Access Values in a Dictionary To access a dictionary value, use square brackets []. For example, the code below uses the key ‘marathon’ to access the value ‘a running race that is about 26 miles’. webstersDict['marathon'] # Get value of the 'marathon' key Access the key ‘marathon’ Keep in mind that you will get a KeyError if you try to access a value for a key that . does not exist webstersDict['nonexistentKey'] # Try to get value for key that does not exist KeyError will result if you try and look up a key that does not exist. In the Dictionary Methods section, you will see the utility of using the dictionary to avoid KeyErrors. method get Add or Update Key You can add a new key-value pair. webstersDict['shoe'] = 'an external covering for the human foot' # add one new key value pair to a dictionary Add the new key ‘shoe’ to the dictionary. The new key ‘shoe’ is enclosed in the red rectangle. You can also update a key-value pair. Update the dictionary key ‘marathon’ You can also add or update multiple key value pairs at a time using the dictionary as we will see later on**.** update method Delete Key from Dictionary It is possible to remove a key and its corresponding value from a dictionary using . del # Remove the key 'resist' from the dictionarydel webstersDict['resist'] Remove the key ‘resist' from the dictionary webstersDict. Dictionary Methods dictionaries have different methods that help you modify a dictionary. This section of the tutorial just goes over various python dictionary methods. Python update method The update method is very useful for updating multiple key values pairs at a time. It takes a dictionary as an argument. webstersDict.update({'ran': 'past tense of run','shoes': 'plural of shoe'}) # Using update method to add two key value pairs at once Added the keys ‘ran’ and ‘shoes’ to the dictionary. If you are wondering why the keys added to the dictionary are not in the order added in, it is because dictionaries are unordered. get method storyCount = {'is': 100,'the': 90,'Michael': 12,'runs': 5} # Define a dictionary The get method returns a value for a given key. If a key doesn’t exist, the dictionary will by default return None. # Since the key 'Michael' exists, it will return the value 12storyCount.get('Michael') Since the key ‘Michael’ exists, it returns the value 12. If ‘Michael’ didn’t exist, it would return None. The method is very useful to look up keys you don’t know are in the dictionary to avoid KeyErrors. They key ‘chicken’ does not exist. You can also specify a default value to return if the key doesn’t exist. storyCount.get('chicken', 0) # Make default value for key that doesn't exist 0. You can see the usefulness of this method if you try a . Python Word Count pop Method The pop method removes a key and returns the value. storyCount.pop('the') Dictionary before and after removing the key ’the’ from the dictionary. keys Method The keys method returns the keys of the dictionary as a list storyCount.keys() Return the keys of the dictionary as a list values Method The values method returns the values in the dictionary as a list storyCount.values() items Method The items method returns a list of tuples where each tuple is of the form (key, value). webstersDict.items() The items method returns a list of tuples in the form of (key, value). Iterate through a Dictionary You can iterate through the keys of a dictionary by using a for loop. for key in storyCount:print(key) Iterate through the keys of the dictionary. You also iterate through the keys of a dictionary by using the keys method. for key in storyCount.keys():print(key) Iterate through the keys of the dictionary. The for loop below uses the items method to access one (key, value) pair on each iteration of the loop. for key, value in webstersDict.items():print(key, value) Iterate through the key, value pairs of a dictionary. If you have difficulty understanding this section, I recommend watching this section of the . video Closing Remarks Please let me know if you have any questions either here or in the comments section of the ! The next post, will review dictionary methods, , and . youtube video Python Word Count list manipulations string manipulations