Skip to main content
HomeAbout PythonLearn Python

Python Dictionaries Tutorial: The Definitive Guide

Learn all about the Python Dictionary and its potential. You will also learn how to create word frequency using the Dictionary.
Mar 2020  · 6 min read

If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course.

Dictionary is a built-in Python Data Structure that is mutable. It is similar in spirit to List, Set, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. On an abstract level, it consists of a key with an associated value. In Python, the Dictionary represents the implementation of a hash-table.

You Might Wonder What Are Keys?

Well, as shown in the figure below, keys are immutable ( which cannot be changed ) data types that can be either strings or numbers. However, a key can not be a mutable data type, for example, a list. Keys are unique within a dictionary and can not be duplicated inside a dictionary, in case if it is used more than once then subsequent entries will overwrite the previous value.

keys and values diagram

Key connects with the value, hence, creating a map-like structure. If you for a second, remove keys from the picture, all you are left with is a data structure containing a sequence of numbers. Dictionaries, therefore, hold a key: value pair at each position.

A dictionary is represented by a pair of curly braces {} in which enclosed are the key: value pairs separated by a comma.

Let's look at the syntax of a dictionary: dictionary = {"key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}#

Unique Keys

Since now you know that keys in a dictionary have to be unique, let's understand it with the help of an example.

dictionary_unique = {"a": "alpha", "o": "omega", "g": "gamma"}

Let's print this out.

{'a': 'alpha', 'o': 'omega', 'g': 'gamma'}

Great, so until now, everything looks fine. You were able to print your first dictionary output. Now, let's repeat the key g with a new value and see what happens.

dictionary_unique = {"a": "alpha", "o": "omega", "g": "gamma", "g": "beta"}
{'a': 'alpha', 'o': 'omega', 'g': 'beta'}

As expected, the key g previous value gamma was overwritten by value beta.

Immutable Keys

Now let's see what happens when you try to define the key as a mutable data type.

dictionary_immutable = {["a","b","c"]: "alpha", "o": "omega", "g": "gamma", "g": "beta"}

TypeError                                 Traceback (most recent call last)

<ipython-input-16-78a55d50cd65> in <module>
----> 1 dictionary_immutable = {["a","b","c"]: "alpha", "o": "omega", "g": "gamma", "g": "beta"}

TypeError: unhashable type: 'list'

From the above output, you can observe that defining the first key of the dictionary as a list results in a TypeError since dictionary keys must be immutable types and list is a mutable type.

However, there is a workaround to it, which is, replacing the list to a tuple, since a tuple is an immutable data type.

dictionary_immutable = {("a","b","c"): "alpha", "o": "omega", "g": "gamma", "g": "beta"}
{('a', 'b', 'c'): 'alpha', 'o': 'omega', 'g': 'beta'}

Accessing Keys and Values

Since you now know how to create dictionaries, let's learn to access the keys and the values from the dictionary. To access the key value pair, you would use the .items() method, which will return a list of dict_items in the form of a key, value tuple pairs.

dict_items([('a', 'alpha'), ('o', 'omega'), ('g', 'beta')])

To access keys and values separately, you could use a for loop on the dictionary or the .keys() and .values() method.

for key, value in dictionary_unique.items():  #accessing keys
for key, value in dictionary_unique.items():  #accessing values
dictionary_unique.keys() #accessing keys without for loop
dict_keys(['a', 'o', 'g'])
dictionary_unique.values()  #accessing values without for loop
dict_values(['alpha', 'omega', 'beta'])

You could even access a value by specifying a key as a parameter to the dictionary.


Nested Dictionary

Creating a nested dictionary is quite simple. In the nested dictionary, you pass in a dictionary inside a dictionary or to put it simply; a dictionary is passed as a value to a key of the main dictionary.

Here you will create a datacamp dictionary inside a nested dictionary dictionary_nested.

dictionary_nested = {"datacamp":{"Deep Learning": "Python", "Machine Learning": "Pandas"},"linkedin":"jobs","nvidia":"hardware"}
{'datacamp': {'Deep Learning': 'Python', 'Machine Learning': 'Pandas'},
 'linkedin': 'jobs',
 'nvidia': 'hardware'}
dictionary_nested['datacamp']['Deep Learning']
dictionary_nested['datacamp']['Machine Learning']

Dictionary Comprehension

Dictionary comprehensions can be used to create dictionaries from arbitrary key and value expressions. It is a simple and concise way of creating dictionaries and is often faster than the usual for loop implementations.

import time
t1 = time.time()
dict_comprehension = {i: i**3 for i in range(200000)}
print(time.time() - t1)

Let's print the first ten key: value pairs from the dict_comprehension dictionary. To achieve this, you will import islice from the itertools built-in package and specify n as a number of key value pairs you want to extract.

from itertools import islice

comp_10 = list(islice(dict_comprehension.items(),10))
[(0, 0), (1, 1), (2, 8), (3, 27), (4, 64), (5, 125), (6, 216), (7, 343), (8, 512), (9, 729)]
import time
t1 = time.time()
dict_comprehension = dict()
for i in range(200000):
    dict_comprehension[i+1] = i**3
print(time.time() - t1)

As you can see from the above two implementations of a dictionary, dictionary comprehension with a minimal margin, is still the winner in terms of the time it takes to run. You would notice that as you keep increasing the range parameter, the difference in the time will also increase.

Word Frequency

From a collection of written texts, in this case, a string of text, also known as corpus, lets us create a word frequency with the help of a dictionary.

corpus = 'learn all about the Python Dictionary and its potential. \
            You would also learn to create word frequency using the Dictionary'
word_freq = dict()
corpus_word = str(corpus).split()
for word in range(len(corpus_ref)):
    if corpus_word[word] not in word_freq:
        word_freq[corpus_word[word]] = 1

         word_freq[corpus_word[word]] += 1
{'learn': 2,
 'all': 1,
 'about': 1,
 'the': 2,
 'Python': 1,
 'Dictionary': 2,
 'and': 1,
 'its': 1,
 'potential.': 1,
 'You': 1,
 'would': 1,
 'also': 1,
 'to': 1,
 'create': 1,
 'word': 1,
 'frequency': 1,
 'using': 1}

Great! So as you can observe from the above output, you were able to get a word count or word frequency from a string of text with the help of a Python Dictionary.


Congratulations on finishing the tutorial.

Please feel free to ask any questions related to this tutorial in the comments section below.

If you are just getting started in Python and would like to learn more, take DataCamp's Introduction to Data Science in Python course and check out our Python Tutorial for Beginners.


Learn more about Python

Certification available


Introduction to Python

4 hr
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See DetailsRight Arrow
Start Course
See MoreRight Arrow

A Deep Dive into the Phi-2 Model

Understanding the Phi-2 model and learning how to access and fine-tune it using the role-play dataset.
Abid Ali Awan's photo

Abid Ali Awan

12 min

Python List Size: 8 Different Methods for Finding the Length of a List in Python

Compare between 8 different methods for finding the length of a list in Python.
Adel Nehme's photo

Adel Nehme

8 min

An End-to-End ML Model Monitoring Workflow with NannyML in Python

Learn an end-to-end workflow to monitor any model in your Jupyter notebook in production environments.
Bex Tuychiev's photo

Bex Tuychiev

15 min

How to Delete a File in Python

File management is a crucial aspect of code handling. Part of this skill set is knowing how to delete a file. In this tutorial, we cover multiple ways to delete a file in Python, along with best practices in doing so.
Amberle McKee's photo

Amberle McKee

5 min

Finding the Size of a DataFrame in Python

There are several ways to find the size of a DataFrame in Python to fit different coding needs. Check out this tutorial for a quick primer on finding the size of a DataFrame. This tutorial presents several ways to check DataFrame size, so you’re sure to find a way that fits your needs.
Amberle McKee's photo

Amberle McKee

5 min

Exploring the Python 'Not Equal' Operator

Comparing values in Python to check if they are not equal is simple with the not equal operator. Check out this quick tutorial on how to use the not equal Python operator, as well as alternatives for comparing floats.
Amberle McKee's photo

Amberle McKee

5 min

See MoreSee More