Python With Spyder 12: Dictionaries

This is the 12th in a series of videos providing a tutorial on Python 2.7 using Anaconda Python and the Spyder IDE. Click here to go to a “home page” for the video series.

This video introduces dictionaries in Python. Dictionaries are Python’s implementation of the idea of key-value pairs.

The source code used in this video can be found here. You can right click on the link and use “Save As” to save the file.

Note: The source code files are plain text files with a “.txt” extention. You will probably want to change the extensions to “.py” after you download them. If you do so, please be aware that if you have Python installed, the file will become executable, so that it will run if you click on it (accidentally or otherwise).

The video is about 31 minutes long.


Next Video: For Loops

Video Index: Dictionaries

Click on the topics below to jump to that location in the video.

Time Topic
00:00Title slide
00:44Creating a dictionary using curly brackets {}.
01:01Key-value pairs separated by the colon (:).
02:06Keys and values do not have to be strings.
02:20Keys and values can be any Python object.
02:27Keys must be unique.
02:40Using a duplicate key overwrites earlier value.
03:05Run the Python code to create the dictionary.
02:12Cells of code in the editor (review)
03:38Run the code in a cell using cntrl-Enter.
03:47Make the Python console larger.
04:06Print out the dictionary in the Python console.
04:37Reference the value corresponding to a key.
05:04Execute a line of code using F9.
05:22Assign a new value corresponding to a key in the dictionary.
06:36Assign a value to a new key not already in the dictionary created a new key-value pair.
07:31Dictionaries in Python do not have an order.
07:51Cannot reference dictionary items using an index.
08:31Dictionaries cannot be sorted.
09:04Second way to create a dictionary starting with an empty dictionary and adding key-value pairs.
09:40Create an empty dictionary using curly brackets {}.
09:53Load key-value pairs into the dictionary by assigning values to new keys.
10:30Virtually everything in Python is an object.
10:43Dictionaries are objects.
10:52The class constuctor for dictionaries in Python is dict()
10:57Create an empty dictionary using dict().
11:17As objects, dictionaries have methods.
11:31Two important dictionary methods are keys() and values()
11:47Example using the keys() method.
12:15The type returned by the keys() method is a list.
12:24Example using the values() method.
12:44The keys and values returned by the key() and values() methods are in the same order.
13:36The pop() method.
13:47pop() method example.
14:38The popitem() method.
14:59popitem() method example.
16:06The type of the key-value pair returned by the popitem() method is a tuple.
16:33Test for a key in the dictionary using the “in” keyword.
16:39Clear the console window using cntrl-L.
16:57Example of using “in” for a key that is in the dictionary.
17:37Example of using “in” for a key that is not in the dictionary.
18:16Using the dir() builtin function to list out all the attributes and methods of a dictionary.
18:35Attributes and methods beginning with a double underscore (__) are intended to be private.
19:05Getting information about dictionary methods using internet search. is the “official” web site for Python.
19:38Finding dictionary (mapping types) documentation in the “Builtin Types” documentation.
20:09Cannot create copies of dictionaries by assignming the dictionary name.
20:17Dictionary name assignment example.
21:24Example of creating a copy of a dictionary using dict().
22:39Use caution when using assignment with dictionary names.
23:22More complicated dictionary example using objects as values.
24:11Example builds on code from the last video: Python With Spyder 11: Inheritence.
24:16Review objects defined in
24:56Execute object definition code from
26:00Create a dictionary with objects as values.
27:49Print the dictionary (from the example with objects as values).
28:28Accessing an attribute of an object in the dictionary.
29:00“AttributeError when accessing an attribute not defined for an object in the dictionary.
29:26Second example of accessing attributes of objects in the dictionary.
30:04Summary of the dictionary example using objects.

Next Video: For Loops

1 thought on “Python With Spyder 12: Dictionaries

Leave a Comment

Time limit is exhausted. Please reload CAPTCHA.