Working with a dictionary of dictionaries

As we mentioned earlier, the real power of these data structures lies in how creatively you can use them to achieve your tasks. Let's look at an example to understand how to use dictionaries in a dictionary.

Getting ready

Look at the following table:

Getting ready

In the first column, we have three users and the rest of the columns are movies. The cell values are ratings given by a user for a movie. Let's say we want to represent this in memory so that some other part of a larger code base can easily access this information. We will use a dictionary of dictionaries to achieve this objective.

How to do it…

We will create the user_movie_rating dictionary using an anonymous function to demonstrate the concept of a dictionary of dictionaries.

We will fill it with data to show the effective use of a dictionary of dictionaries:

from collections import defaultdict

user_movie_rating = defaultdict(lambda :defaultdict(int))

# Initialize ratings for Alice
user_movie_rating["Alice"]["LOR1"] =  4
user_movie_rating["Alice"]["LOR2"] =  5
user_movie_rating["Alice"]["LOR3"] =  3
user_movie_rating["Alice"]["SW1"]  =  5
user_movie_rating["Alice"]["SW2"]  =  3
print user_movie_rating

How it works…

The user_movie_rating is a dictionary of dictionaries. As explained in the previous section, defaultdict takes a function for argument; in this case, we passed a built-in anonymous function, lambda, which returns a dictionary. So, every time a new key is passed to user_movie_rating, a new dictionary will be created for this key. We will see more about the lambda function in the subsequent section.

This way, we can access the rating of any user movie combination very quickly. Similarly, there are plenty of use cases where a dictionary of dictionaries comes in very handy.

As a closing note on the dictionary, I would like to mention that having a good grasp of the dictionary data structure will help ease a lot of your data science programming tasks. As we will see later, dictionaries are frequently used to store features and labels in machine learning. The Python NLTK library uses a dictionary extensively to store features in text mining:

http://www.nltk.org/book/ch05.html

The section titled Mapping words to Properties using Python Dictionaries is a good read to understand how effectively dictionaries can be used.

See also

  • Creating Anonymous Functions 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