Using dictionary objects

In Python, containers are objects that can hold any number of arbitrary objects. They provide a way to access the child objects and iterate over them. Dictionary, tuple, list, and set are container objects in Python. More container types are available with the collections module. Let's look at the dictionary object in detail in this section.

Getting ready

Let's look at an example Python script to understand how a dictionary operates. So, with a text, this script tries to get the word count, that is, how many times each word has appeared in the given text.

How to do it…

Let's proceed to demonstrate how to operate a dictionary in Python. Let's use a simple sentence to demonstrate the use of a dictionary. We will follow it up with an actual dictionary creation:

# 1.Load a variable with sentences
sentence = "Peter Piper picked a peck of pickled peppers A peck of pickled 
peppers Peter Piper picked If Peter Piper picked a peck of pickled 
peppers Wheres the peck of pickled peppers Peter Piper picked"


# 2.Initialize a dictionary object
word_dict = {}

# 3.Perform the word count
for word in sentence.split():
    if word not in word_dict:
        word_dict[word] =1
    else:  
        word_dict[word]+=1
# 4.print the outputprint (word_dict)

How it works…

The preceding code builds a word frequency table; every word and its frequency is calculated. The final print statement produces the following output:

{'a': 2, 'A': 1, 'Peter': 4, 'of': 4, 'Piper': 4, 'pickled': 4, 'picked': 4, 'peppers': 4, 'the': 1, 'peck': 4, 'Wheres': 1, 'If': 1}

The preceding output is a key value pair. For each word (key), we have a frequency (value). A dictionary data structure is a hash map where values are stored against a key. In the preceding example, we used a string as a key; however, any other immutable data type can also be used as a key.

Refer to the following URL for a detailed discussion about mutable and immutable objections in Python:

https://docs.python.org/2/reference/datamodel.html

Similarly, values can be any data type including custom classes.

In step 2, we initialized the dictionary. Its empty when initialized. When a new key is added to a dictionary, accessing the dictionary through the new key will throw KeyError. In the preceding example in step 3, we included an if statement in the for loop to handle this situation. However, we can also use the following:

word_dict.setdefault(word,0)

With every key access to the dictionary, this statement has to be repeated if we are adding elements to a dictionary in a loop, as in a loop, we are not aware of new keys. Rewriting step 3 using setdefault will look as follows:

for word in sentence.split():
word_dict.setdefault(word,0)
word_dict[word]+=1

There's more…

Python 2.5 and above has a class named defaultdict; it's in the collections module. This takes care of the setdefault action. A defaultdict class is invoked as follows:

from collections import defaultdict

sentence = "Peter Piper picked a peck of pickled peppers  A peck of pickled 
            peppers Peter Piper picked If Peter Piper picked a peck of pickled 
            peppers Wheres the peck of pickled peppers Peter Piper picked"

word_dict = defaultdict(int)

for word in sentence.split():
    word_dict[word]+=1print word_dict   

As you have noticed, we included collections.defaultdict in our code and initialized our dictionary. Note that the int parameter, defaultdict, takes a function as an argument. In this case, we passed the int() function and thus, when the dictionary encounters a key that was not seen before, it initializes the key with a value returned by the int() function, in this case, zero. We will use defaultdict later in this book.

Note

A typical dictionary does not remember the order in which the keys were inserted. In its collections module, Python provides a container called OrderedDict that can remember the order in which the keys were inserted. See the following Python documentation for more details:

https://docs.python.org/2/library/collections.html#collections.OrderedDict

Looping through a dictionary is very easy; using the keys() function provided in the dictionary, we can loop through the key and using values(), we can loop through the values or using items(), we can loop through both the keys and values. Look at the following example:

For key, value in word_dict.items():
print key,value

In this example, using dict.items(), we can iterate through the keys and values present in the dictionary.

The Python documentation for dictionaries is very exhaustive and is a handy companion when working with dictionaries:

https://docs.python.org/2/tutorial/datastructures.html#dictionaries

Dictionaries are very useful as an intermediate data structure. If your program uses JSON as a way to move around information between modules, dictionary is the right data type for the job. It is very convenient to load a dictionary from a JSON file and similarly dump a dictionary as JSON strings.

Python provides us with libraries to handle JSON very efficiently:

https://docs.python.org/2/library/json.html

Counter is a dictionary subclass to count the hashable objects. Our example of the word count can be easily done using counter.

Look at the following example:

from collections import Counter

sentence = "Peter Piper picked a peck of pickled peppers  A peck of pickled 
            peppers Peter Piper picked If Peter Piper picked a peck of pickled 
            peppers Wheres the peck of pickled peppers Peter Piper picked"

words = sentence.split()

word_count = Counter(words)

print word_count['Peter']print word_dict   

The output is as follows and you can verify this output with the previous one:

Counter({'Peter': 4, 'of': 4, 'Piper': 4, 'pickled': 4, 'picked': 4, 'peppers': 4, 'peck': 4, 'a': 2, 'A': 1, 'the': 1, 'Wheres': 1, 'If': 1})

You can go through the following link to understand more about Counters:

https://docs.python.org/2/library/collections.html#collections.Counter

See also

  • Working with Dictionary of Dictionaries recipe in Chapter 1, Using Python for Data Science
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset