Overview of Dictionaries in Python-2

 

To be successful you should follow these tasks weekly

 

    read the overview page in each assigned module

    read the lecture notes posted in modules

    participate in the discussion forums

    review the supplement documents and example code posted in the modules

    attempt the practice exercise in the module

    complete the module assignments

    complete the module quiz

 

Important Reminder

The correct process for responding to discussions is to first post your own reply to the discussion topic.  Other participant’s replies will not be visible until you post your first individual reply.  Second, you must post a brief reply to at least two other students’ replies. This along with a relevant individual reply will give you full credit in discussion assignments.

 

 

 

 

 A dictionary is an object that stores a collection of data. Each element in a dictionary has two parts: a key and a value. You use a key to locate a specific value.

 

 In Python, a dictionary is an object that stores a collection of data. Each element that is stored in a dictionary has two parts: a key and a value. In fact, dictionary elements are commonly referred to as key-value pairs. When you want to retrieve a specific value from a dictionary, you use the key that is associated with that value. This is similar to the process of looking up a word in the Merriam-Webster dictionary, where the words are keys and the definitions are values

 

The textbook provides the following example

 

 For example, suppose each employee in a company has an ID number, and we want to write a program that lets us look up an employee’s name by entering that employee’s ID number. We could create a dictionary in which each element contains an employee ID number as the key and that employee’s name as the value. If we know an employee’s ID number, then we can retrieve that employee’s name.

Key-value pairs are often referred to as mappings because each key is mapped to a value.

Creating a Dictionary

You can create a dictionary by enclosing the elements inside a set of curly braces ( {} ).

An element consists of a key, followed by a colon, followed by a value. The elements are separated by commas.

The values in a dictionary can be objects of any type, but the keys must be immutable objects.

Retrieving a Value from a Dictionary

The elements in a dictionary are not stored in any particular order.

dictionaries are not sequences, like lists, tuples, and strings. As a result, you cannot use a numeric index to retrieve a value by its position from a dictionary. Instead, you use a key to retrieve a value.

 

To retrieve a value from a dictionary, you simply write an expression in the following general format:

dictionary_name[key]

In the general format, dictionary_name is the variable that references the dictionary, and key is a key. If the key exists in the dictionary, the expression returns the value that is associated with the key. If the key does not exist, a KeyError exception is raised.

 

Using the in and not in Operators to Test for a Value in a Dictionary

As previously demonstrated, a KeyError exception is raised if you try to retrieve a value from a dictionary using a nonexistent key. To prevent such an exception, you can use the in operator to determine whether a key exists before you try to use it to retrieve a value.

 

 

Adding Elements to an Existing Dictionary

Dictionaries are mutable objects. You can add new key-value pairs to a dictionary with an assignment statement in the following general format: dictionary_name[key] = value

 

In the general format, dictionary_name is the variable that references the dictionary, and key is a key. If key already exists in the dictionary, its associated value will be changed to value. If the key does not exist, it will be added to the dictionary, along with value as its associated value.

You cannot have duplicate keys in a dictionary: When you assign a value to an existing key, the new value replaces the existing value.

 

Deleting Elements

You can delete an existing key-value pair from a dictionary with the del statement. Here is the general format:

del dictionary_name[key]

In the general format, dictionary_name is the variable that references the dictionary, and key is a key. After the statement executes, the key and its associated value will be deleted from the dictionary. If the key does not exist, a KeyError exception is raised.

 

Getting the Number of Elements in a Dictionary

You can use the built-in len function to get the number of elements in a dictionary.

Mixing Data Types in a Dictionary

As previously mentioned, the keys in a dictionary must be immutable objects, but their associated values can be any type of object. For example, the values can be lists

 

 

Creating an Empty Dictionary

Sometimes you need to create an empty dictionary and then add elements to it as the program executes. You can use an empty set of curly braces to create an empty dictionary.

This is illustrated in the following code

phonebook = {}

 

Use the for Loop to Iterate over a Dictionary

You can use the for loop in the following general format to iterate over all the keys in a dictionary:

for var in dictionary:

statement

statement

In the general format, var is the name of a variable and dictionary is the name of a dictionary. This loop iterates once for each element in the dictionary. Each time the loop iterates, var is assigned a key

 

Review the assortment of directory methods in the Chapter

 

 

 

 

 

 

 

Homework

 

Readings

 

Discussions for the Week

None

Weekly Quiz