Comprehensive Guide to Python Dictionaries

Learn about Python dictionaries, a versatile data structure used for storing data values in key-value pairs. Discover how to create dictionaries, their characteristics, and their benefits, including their order, mutability, and handling of duplicate values.



Python Dictionaries

Dictionaries are used to store data values in key:value pairs. They are collections that are ordered (as of Python 3.7), changeable, and do not allow duplicates.

Creating a Dictionary

Dictionaries are written with curly brackets and have keys and values:

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict)
Output

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

Dictionary Items

Dictionary items are ordered, changeable, and do not allow duplicates. Items are presented in key:value pairs and can be referred to by using the key name.

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(thisdict["brand"])
Output

Ford

Ordered or Unordered?

As of Python version 3.7, dictionaries are ordered, meaning the items have a defined order that will not change. In Python 3.6 and earlier, dictionaries are unordered.

Changeable

Dictionaries are changeable, meaning you can change, add, or remove items after the dictionary has been created.

Duplicates Not Allowed

Dictionaries cannot have two items with the same key. Duplicate values will overwrite existing values.

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964,
  "year": 2020
}
print(thisdict)
Output

{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}

Dictionary Length

To determine the number of items in a dictionary, use the len() function:

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(len(thisdict))
Output

3

Dictionary Items - Data Types

The values in dictionary items can be of any data type:

Example

thisdict = {
  "brand": "Ford",
  "electric": False,
  "year": 1964,
  "colors": ["red", "white", "blue"]
}
print(thisdict)
Output

{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red', 'white', 'blue']}

Type

From Python's perspective, dictionaries are defined as objects with the data type 'dict':

Example

thisdict = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}
print(type(thisdict))
Output

<class 'dict'>

The dict() Constructor

It is also possible to use the dict() constructor to make a dictionary:

Example

thisdict = dict(name="John", age=36, country="Norway")
print(thisdict)
Output

{'name': 'John', 'age': 36, 'country': 'Norway'}

Python Collections (Arrays)

There are four collection data types in Python:

  • List: Ordered and changeable. Allows duplicate members.
  • Tuple: Ordered and unchangeable. Allows duplicate members.
  • Set: Unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary: Ordered** and changeable. No duplicate members.

*Set items are unchangeable, but you can remove and/or add items.

**As of Python 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Choosing the right collection type for a data set can improve efficiency or security and retain meaning.