DICTIONARIES

Credit: The information below has been obtained from http://introtopython.org and used with permission under the MIT license. 

In Python, dictionaries are a way to store information that is connected in some way. Dictionaries store information in key-value pairs, so that any one piece of information in a dictionary is connected to at least one other piece of information. Think of how a printed dictionary works. There is a word followed by a definition. They are linked together where the reader can look up a word and find the associated definition attached to it. It works very similar in Python.

Sounds like a list right? In lists, we linked a list index to a value. For instance:

[python light=”true”]my_list[0] = "Value_1"[/python]

In this example, the index 0 is associated with the value Value_1

Dictionaries do much the same thing, only they use a key instead of an index. Also, dictionaries do not store their information in any particular order, so you may not get your information back in the same order you entered it.

SYNTAX

In Python, a dictionary is setup using the following syntax:

[python light=”true”]dictionary_name = {key1: value_1, key2: value_2, key3: value_3}[/python]

Notice the use of the curly brackets instead of the square brackets, like the ones used in a list. Also, each value is not given an index value but is rather assigned a key.

Since the keys and values in dictionaries can be long, we often write just one key-value pair on a line. You might see dictionaries that look more like this:

[python]
dictionary_name = {key_1: value_1,
key_2: value_2,
key_3: value_3
}
[/python]

This is a bit easier to read, especially if the values are long.

Here you can create the content that will be used within the module.

EXAMPLES

A simple example of a Python dictionary involves modelling an actual dictionary, where we assign a definition to a key word.

[python]

python_words = {‘list’: ‘A collection of values that are not connected, but have an order.’,
‘dictionary’: ‘A collection of key-value pairs.’,
‘function’: ‘A named set of instructions that defines a set of actions in Python.’,
}

[/python]

We can get individual items out of the dictionary, by giving the dictionary’s name, and the key in square brackets:

Try the program below to learn how to access values within a dictionary.

This code looks pretty repetitive, and it is. Dictionaries have their own for-loop syntax, but since there are two kinds of information in dictionaries, the structure is a bit more complicated than it is for lists. We will learn how to loop through a dictionary a little bit later.

To ensure that you understand how dictionaries work, try the following in the code above:

  • Change the keys and see if you can update the code to access the value
  • Change the value and see what happens when you call on it’s key.

EXERCISES

Pet Names

  • Create a dictionary to hold information about pets. Each key is an animal’s name, and each value is the kind of animal.
    • For example, ‘ziggy’: ‘canary’
  • Put at least 3 key-value pairs in your dictionary.
  • Print each entry in the dictionary in a series of statements such as “Willie is a dog”.

Polling Friends

  • Think of a question you could ask your friends. Create a dictionary where each key is a person’s name, and each value is that person’s response to your question.
  • Store at least three responses in your dictionary.
  • Print out a series of statements listing each person’s name, and their response.