Mapping Data Type — Dictionary

code guest
Python Journey
Published in
4 min readAug 13, 2019

We know that strings store characters in sequence while lists and tuples store data values of any data types in sequence.

Python has another data type which can store multiple data values called Dictionary.

To understand dictionaries better, we will compare a list [“one”, “uno”, “two”, “dos”] with a dictionary {“one”:”uno”, “two”: “dos”}.

Note: uno is one and dos is two in Spanish

  1. Lists are enclosed by square brackets while dictionaries are enclosed by curly braces
  2. Lists store data values individually like “one” followed by “uno”, while dictionaries store key-value combinations like “one”:”uno” followed by “two”:”dos”
  3. List data values are retrieved by their index (eg. [“one”, “uno”, “two”, “dos”][1] gives us “uno”) while dictionaries values are retrieved by their key (eg. {“one”:”uno”, “two”: “dos”}[“one”] gives us “uno”)

Dictionary keys and values

As you would have noticed, dictionaries introduce something called keys which are pretty useful because we can link two related things together like “one” with “uno” and use the key “one” to retrieve its other half “uno”.

Here are some additional points to note about keys and values in dictionaries:

  • A value is always stored with a key like “one”:”uno”
  • A key is paired with a value by a colon
  • key-value pairs are separated by commas
  • A key is mapped to its value — which is why we retrieve a value based on its key and also why dictionaries are a mapping data type
  • Keys must be immutable (which means they cannot be modified after creation — keys can be strings, tuples etc)
  • Values are mutable though
  • Values can be of any data type

Note: If you are curious why keys must be immutable, see this stackoverflow post.

Dictionaries store information in key-value pairs (credits: undraw.co)

Why do we use dictionaries?

Because we can store information in key-value pairs such as name-address and easily look up a stored value by its key.

Given a choice between strings, lists, tuples or dictionaries, which would you choose to implement the following?

  • Contact list of friends and their phone numbers
  • Santa’s Christmas delivery list with addresses and corresponding presents
  • Students’ grades for an exam

There are a really wide range of use cases for dictionaries and you will no doubt be familiar with them as you progress in your python journey!

How do dictionaries work?

Under the hood, dictionaries make use of a clever technique called hash function to convert keys (regardless of its length or data type — as long as it is immutable) to unique numbers.

Hash function convert keys or any length to unique numbers (credits: wikimedia.org)

Oversimplified example → in {“one”:”uno”, “two”: “dos”}, the key “one” gets converted to integer 2 by the hash function and the key “two” gets converted to integer 4

The unique numbers and their corresponding value is then stored in a table called a hash table.

Oversimplified example: key “one” gets hashed to integer 2 and is stored in hash table with corresponding value “uno”

And then, when we try to retrieve the stored value from the dictionary using the key “one”, Python converts the key we are querying to its hash (ie. “one” to 2), looks up the hash table to see which value corresponds to the hash number 2 and retrieves “uno”.

Summary

  • A mapping data type called dictionary stores multiple data values
  • Unlike strings/lists/tuples, dictionaries store key-value pairs
  • Keys must be immutable
  • The implementation of dictionaries rely on hash table

Good reads

Quiz

Which of the following options are incorrect?

a) “one”:”uno”, “two”:”dos” is a dictionary

b) [123] is not a suitable key

c) {123:[“yellow”, ”cats”], (10,”Manila Street”):”sweet shop!!!”} is a valid dictionary

d) This code {“one”:”uno”, “two”: “dos”}(“two”) lets us retrieve “dos”

Quiz explanation

a) Dictionaries are enclosed by curly braces

b) keys must be immutable

c) {123:[“yellow”, ”cats”], (10,”Manila Street”):”sweet shop!!!”} is a valid dictionary. Keys 123 — integer and (10,”Manila Street”) — tuple are immutable while values can be of any data type.

d) To retrieve a dictionary value using a key, we need to enclose the key in square brackets.

Quiz answers: a & d

--

--