Skip to main content

A dictionary holds data as key:value pairs.

This kind of collection keeps its order, can be changed, and refuses duplicates.

You write one with curly brackets, listing its keys and values inside:

Example

Build a dictionary and print it:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964 respectively. Then print the dictionary.
Output: complete Python program

Dictionary Items

Items in a dictionary keep their order, can be modified, and cannot be duplicated.

Each item is a key:value pair, and the key's name is how you get to it.

Example

Print the value stored under "brand":

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964 respectively. Then print the value of the "brand" key.
Output: complete Python program

Ordered or Unordered?

An ordered dictionary has a fixed sequence of items that stays the same.

An unordered collection has no defined sequence, so you cannot point to an item by its index.

Note: Dictionaries have been ordered since Python version 3.7. Up to and including Python 3.6, they were unordered.


Changeable

A dictionary stays open to edits after you create it: items can be changed, added or removed.


Duplicates Not Allowed

Each key can appear only once in a dictionary:

Example

Repeating a key overwrites the earlier value:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", "year", and "year" (duplicate) with values "Ford", "Mustang", 1964, and 2020 respectively. Then print the dictionary to show that the duplicate key's value is overwritten.
Output: complete Python program

Dictionary Length

Call the len() function to find out how many items a dictionary holds:

Example

Count the dictionary's items and print the total:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964 respectively. Then print the number of items in the dictionary using the len() function.
Output: complete Python program

Dictionary Items - Data Types

A dictionary value can have any data type you like:

Example

Values that are a string, a boolean, an int and a list:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand" (string "Ford"), "electric" (boolean False), "year" (integer 1964), and "colors" (list ["red", "white", "blue"]). Then print the dictionary.
Output: complete Python program

type()

Python treats every dictionary as an object whose data type is 'dict':

Example

Ask Python for the type of a dictionary:

Prompt
Write a Python program that creates a dictionary called thisdict with keys "brand", "model", and "year" with values "Ford", "Mustang", and 1964 respectively. Then print the data type of thisdict using the type() function.
Output: complete Python program

The dict() Constructor

You can also build a dictionary by calling the dict() constructor.

Example

Build a dictionary with the dict() constructor:

Prompt
Write a Python program that creates a dictionary called thisdict using the dict() constructor with name="John", age=36, and country="Norway". Then print the dictionary.
Output: complete Python program