In Python, a dictionary is a built-in data type that can be used to store data in a way thats different from lists or arrays. Dictionaries aren't sequences, so they can't be indexed by a range of numbers, rather, they're indexed by a series of keys. When learning about dictionaries, it's helpful to think of dictionary data as unordered key: value pairs, with the keys needing to be unique within a single dictionary. You can create a dictionary easily within a pair of curly braces. Fill the braces with data with values in the key: value format, separating your key: value pairs with commas, and you've got yourself a dictionary.

See the code below for an example of what a dictionary looks like and how it should be formatted:

example = {'key1': value1, 'key2': value2, 'key3': value3}

If your keys are strings, it's important to remember to put them in quotation marks.

What's interesting about dictionaries are all the different ways that they can be used to map data. A common dictionary is operation is to extract the values from the key: value pairs and convert them to a list, the code for which is actually quite straightforward. To see how it's done, check out the code snippet below.

To start, we'll need a dictionary to work with:

food = {'pizza': 324, 'sandwich': 78, 'hot dog': 90}

Next, we'll need the code that can be used to execute the conversion of the values from the dictionary to a list, like this:


That's it. The output of the code above should be the data values of the key: value pairs from your dictionary, in the exact order that they appear in your dictionary. So your output should look something like this:

324, 78, 90

You can use this code snippet to print the values of the key: value pairs of any dictionary as a list.


To Practice: Try this interactive course on the basics of Lists, Functions, Packages and NumPy in Python.