Using Dictionaries

Must Read

Just post!

Dictionaries take a different approach to storing information. Every other structure covered in this chapter uses some type of numeric index — a dictionary relies on a key. Using a key does increase the memory footprint of a dictionary so you don’t want to use a dictionary all the time, even though the quick access of a key may seem quite attractive. The key does make it possible to access elements considerably faster. Listing 4-13 shows a dictionary in use.

Listin g 4-13: Accessing data using a dictionary


# Create the dictionary.
PeopleColors = {‘George’:’Red’, ‘Ann’:’Purple’, ‘Sam’:’Yellow’}
# Randomly access a value.
print ‘The color George likes best is’, PeopleColors[‘George’]
# Add a new person and color.
PeopleColors[‘Nancy’] = ‘Blue’
# Verify that the new person was added.
if PeopleColors.has_key(‘Nancy’):
print ‘Added Nancy with color’, PeopleColors[‘Nancy’]
# Iterate through all of the values.
print ‘nHere are the colors people like:’
for Key, Value in PeopleColors.iteritems():
print Key, ‘likes’, Value
# Pause after the debug session.
raw_input(‘Press any key to continue…’)


Use a deque when you need the extra left-side flexibility this structure can provide

Creating a dictionary is different from other sorts of storage types. Notice that you enclose all of the values in curly braces ({}). The key appears first and the value second. You separate key from value using a colon (:). The key is normally a string, but the value can be anything. The example could just as easily have used numbers.

Accessing a particular dictionary element is different, too. You don’t need to know an index number — you simply need to know the value you want. In this case, the example shows the color that George likes best. The code doesn’t need to know that George appears first in the dictionary (he actually doesn’t, but more about that in a moment).

Adding new members to a dictionary is easy. You simply provide a new key and assign a value to it, as shown in the code. If you want to verify that a particular key appears in the dictionary, simply use the has_key() method. It’s important to remember that dictionaries use methods different from those of other storage techniques. For example, there’s no append() method when working with a dictionary. Interestingly enough, a dictionary does provide the pop() method.

Iterating through a dictionary (listing its content) is also a bit different. You still use a for loop to perform the task. However, notice that you use the iteritems() method to obtain a list of key/ value pairs from the dictionary. Figure 4-12 shows the output from this example.

Dictionaries are best used for named data.

- Advertisement -

Latest News

Elevate Your Bentley Experience: The Bespoke Elegance of Bentayga EWB by Mulliner

Bentley Motors redefines the essence of bespoke luxury with the introduction of the Bentayga EWB's groundbreaking two-tone customization option—a...
- Advertisement -

More Articles Like This

- Advertisement -