• 4 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/15/21

Explore the dictionary data type

The map or dictionary data type, stores key/value pairs and can be thought of as a real dictionary. You associate word/definition pairs with a dictionary. With the map container, the word is the key, and the definition is the associated object that you want to store. Like a list, a map can grow dynamically as you add more pairs of key/values to it. It also allows you to "look up" a value for a given key.

Dictionary: words and definitions
Dictionary: words and definitions

Dictionaries are particularly useful when you need to store data that will be looked up again and again. Suppose you are creating a new object-oriented dictionary and would like to use a map data type to store your definitions.  

The key value pairs for your dictionary could look something like this:

Key (word)

Value (definition)

short

lasting a small amount of time

long

measuring a great distance from end to end

now

 at the present time or moment 

Acme Egg Company social media

Acme Egg Company would like you to develop a comments page that will allow customers to give feedback.  Customer have something to say and they want to constantly improve the egg operations.  Here is what they have told us so far:

  1. Customers can enter several comments per day.

  2. They want to comments to be logged by when they were posted.

To do this, you could create a key containing the year, month, then the day the comment was posted.  Your value would then be the text of the comment itself. It could look something like:

Key (when)

Value (comment)

20180407

I purchase eggs from this firm every Tuesday and the service is always the greatest!

20180107

Their sales department is the best I’ve seen.  I purchase from many corporations and this shipping system is wonderful.

20180510

The grade A large eggs are the best we have tasted. We also use them for baking cakes.

You can add on as many of these comments as you need, but if you find out some of the comments are from co-workers joking around, you can delete those!  A dictionary is flexible that way. 😉

Summary

  • The map or dictionary data type stores rows of key/value pairs.

  • Rows can be added/removed for any key you wish.

  • Size is dynamic, and can shrink/grow in real time. 

Example of certificate of achievement
Example of certificate of achievement