Skip to main content

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.

Introduction to Python

4 hours
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

Intermediate Python

4 hours
Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Python Data Science Toolbox (Part 1)

3 hours
Learn the art of writing your own functions in Python, as well as key concepts like scoping and error handling.
See all coursesRight Arrow

The 23 Top Python Interview Questions & Answers

Essential Python interview questions with examples for job seekers, final-year students, and data professionals.
Abid Ali Awan's photo

Abid Ali Awan

22 min

Working with Dates and Times in Python Cheat Sheet

Working with dates and times is essential when manipulating data in Python. Learn the basics of working with datetime data in this cheat sheet.
DataCamp Team's photo

DataCamp Team

Plotly Express Cheat Sheet

Plotly is one of the most widely used data visualization packages in Python. Learn more about it in this cheat sheet.
DataCamp Team's photo

DataCamp Team

0 min

Getting started with Python cheat sheet

Python is the most popular programming language in data science. Use this cheat sheet to jumpstart your Python learning journey.
DataCamp Team's photo

DataCamp Team

8 min

Python pandas tutorial: The ultimate guide for beginners

Are you ready to begin your pandas journey? Here’s a step-by-step guide on how to get started. [Updated November 2022]
Vidhi Chugh's photo

Vidhi Chugh

15 min

Python Iterators and Generators Tutorial

Explore the difference between Python Iterators and Generators and learn which are the best to use in various situations.
Kurtis Pykes 's photo

Kurtis Pykes

10 min

See MoreSee More