Here’s everything you need to know about the Dictionary data type in Python, what it can store, and how to use it.
Python has several built-in data types for storing data (more than other languages like JavaScript) – from basic types like integers, floating-point numbers, and strings to more complex types like lists, tuples, and dictionaries.
What is a Type?
A variable’s type defines what kind of value it can store and what can be done with it.
What is a Dictionary?
A dictionary is a variable type that is available in Python. In other languages, they may be referred to as associative arrays.
A dictionary contains a collection of objects – but unlike a list or array, rather than their values being recorded at a specific index (or ordered position in the sequence of stored values), they are stored in a key – a string identifier which both tells you what a value is for, and which can also be used to retrieve it.
Put succinctly; a dictionary is a variable that contains a collection of key:value pairs.
If you’re coming from PHP, you will have seen this implemented as associative arrays. If you’re coming from a JavaScript background, simple javascript objects are similar in that they also store data in key:value pairs.
Dictionaries are Ordered and Changeable
In older versions of Python prior to version 3.7, dictionaries were unordered – there was no defined order, and items were subject to change position.
This is no longer the case! Dictionaries are now ordered – the contents of the dictionary will remain in the order they are defined or added.
Unlike Tuples, dictionaries are changeable – you can add, remove and alter the items within.
Keys Must Be Unique!
As each key is used to access a specific value, there cannot be duplicates (otherwise, how is Python to know which key you mean to access?).
Creating a Dictionary / Dictionary Syntax
Creating a dictionary in Python is as simple as declaring a variable conforming to the dictionary type’s syntax:
myDictionary = { "name": "Fred", "animal": "cat", "colour": "red", "age": 3 }
A dictionary is defined by creating a collection of key:value pairs wrapped in curly braces ({}). *As you can see, this dictionary describes someone’s pet cat. *
In the above example, a series of keys (name, animal, color, age) are assigned different values (Fred, cat, red, 3).
The values can be anything – strings, integers, objects, other dictionaries.
The keys must be strings – they are used to describe and access the values. It’s easy to see what data is stored in the dictionary simply by looking at the keys – we know the value red describes the color of the cat.
Accessing Dictionary Values
The entirety of a dictionary variable can be accessed by the variable name; for example – the below line of Python will print the dictionary, and the values contained:
print(myDictionary)
The values stored inside the dictionary can be accessed by their key – append the key, surrounded by [] (square brackets) to access the value stored at that key:
print(myDictionary["colour"]) # will print "red"
If the key you are trying to read from is not defined, you will receive a KeyError.
Updating The Dictionary
Updating values for a given key is simply a matter of overwriting them:
myDictionary\["colour"\] = "blue"
Removing Items from a Dictionary
The delete statement is used to remove a key:value pair from a dictionary:
del myDictionary\["colour"\]
Adding Items to a Dictionary
There are a couple of ways to add items to a Dictionary, which we have covered in the article here.