Introduction to Dictionaries in Python

Andreas Soularidis
Python in Plain English
8 min readSep 20, 2021

Photo by Chris Ried on Unsplash

Hi everyone, in this article, we will talk about Python dictionaries. This is the third article in a row where we talk about built-in data types in Python. You can read about Python lists and Python tuples.

Description

A dictionary is used to store data in the form of key: value pairs. The elements of a dictionary are ordered (from Python version 3.7), so the items have a defined order, that will not change.

Moreover, dictionaries in Python are changeable, which means that we can add, update or delete items after the creation of a dictionary. Be careful that the values of a dictionary are mutable, not the keys. If we try to change the value of a key we will get an error.

Another feature of dictionaries is that they don’t allow duplicate values, so we cannot have more than one element with the same key. From the Python perspective, a dictionary is defined as an object of class dict. You can check it using the function type().

Create a Dictionary

We can create a dictionary with various ways, but the most common of them is by using curly brackets having keys and values separated by commas, as follows:

my_dict = {
"key1" : value1,
"key2: : value2,
...
"keyn" : valuen
}

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Published in Python in Plain English

New Python content every day. Follow to join our 3.5M+ monthly readers.

Written by Andreas Soularidis

Hi, my name is Andreas and I'm passionate about Programming, Algorithms, Machine Learning, Semantic Web, and IoT.

No responses yet

What are your thoughts?