Python Dictionaries

Python Dictionaries


Dictionary

Dictionaries are used to store data values in key:value pairs.

A dictionary is a collection which is ordered*, changeable and do not allow duplicates.

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

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

Example

Create and print a dictionary:

Try it Yourself »


Dictionary Items

Dictionary items are ordered, changeable, and does not allow duplicates.

Dictionary items are presented in key:value pairs, and can be referred to by using the key name.

Example

Print the “brand” value of the dictionary:

Try it Yourself »


Ordered or Unordered?

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

When we say that dictionaries are ordered, it means that the items have a defined order, and that order will not change.

Unordered means that the items does not have a defined order, you cannot refer to an item by using an index.


Changeable

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


Duplicates Not Allowed

Dictionaries cannot have two items with the same key:

Example

Duplicate values will overwrite existing values:

Try it Yourself »


Dictionary Length

To determine how many items a dictionary has, use the len() function:

Example

Print the number of items in the dictionary:

Try it Yourself »


Dictionary Items – Data Types

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

Example

String, int, boolean, and list data types:

Try it Yourself »


type()

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

<class ‘dict’>

Example

Print the data type of a dictionary:

Try it Yourself »


Python Collections (Arrays)

There are four collection data types in the Python programming language:

  • List is a collection which is ordered and changeable. Allows duplicate members.
  • Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
  • Dictionary is a collection which is ordered** and changeable. No duplicate members.

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

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

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.

Python – Access Dictionary Items


Accessing Items

You can access the items of a dictionary by referring to its key name, inside square brackets:

Example

Get the value of the “model” key:

Try it Yourself »

There is also a method called get() that will give you the same result:

Example

Get the value of the “model” key:

Try it Yourself »


Get Keys

The keys() method will return a list of all the keys in the dictionary.

Example

Get a list of the keys:

Try it Yourself »

The list of the keys is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the keys list.

Example

Add a new item to the original dictionary, and see that the keys list gets updated as well:

Try it Yourself »


Get Values

The values() method will return a list of all the values in the dictionary.

Example

Get a list of the values:

Try it Yourself »

The list of the values is a view of the dictionary, meaning that any changes done to the dictionary will be reflected in the values list.

Example

Make a change in the original dictionary, and see that the values list gets updated as well:

Try it Yourself »

Example

Add a new item to the original dictionary, and see that the values list gets updated as well:

Try it Yourself »


Get Items

The items() method will return each item in a dictionary, as tuples in a list.

Example

Get a list of the key:value pairs

Try it Yourself »

The returned list is a view of the items of the dictionary, meaning that any changes done to the dictionary will be reflected in the items list.

Example

Make a change in the original dictionary, and see that the items list gets updated as well:

Try it Yourself »

Example

Add a new item to the original dictionary, and see that the items list gets updated as well:

Try it Yourself »


Check if Key Exists

To determine if a specified key is present in a dictionary use the in keyword:

Example

Check if “model” is present in the dictionary:

Try it Yourself »

Python – Change Dictionary Items


Change Values

You can change the value of a specific item by referring to its key name:

Example

Change the “year” to 2018:

Try it Yourself »


Update Dictionary

The update() method will update the dictionary with the items from the given argument.

The argument must be a dictionary, or an iterable object with key:value pairs.

Example

Update the “year” of the car by using the update() method:

Try it Yourself »

Python – Change Dictionary Items


Change Values

You can change the value of a specific item by referring to its key name:

Example

Change the “year” to 2018:

Try it Yourself »


Update Dictionary

The update() method will update the dictionary with the items from the given argument.

The argument must be a dictionary, or an iterable object with key:value pairs.

Example

Update the “year” of the car by using the update() method:

Try it Yourself »

Python – Remove Dictionary Items


Removing Items

There are several methods to remove items from a dictionary:

Example

The pop() method removes the item with the specified key name:

Try it Yourself »

Example

The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):

Try it Yourself »

Example

The del keyword removes the item with the specified key name:

Try it Yourself »

Example

The del keyword can also delete the dictionary completely:

Try it Yourself »

Example

The clear() method empties the dictionary:

Try it Yourself »

Python – Loop Dictionaries


Loop Through a Dictionary

You can loop through a dictionary by using a for loop.

When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.

Example

Print all key names in the dictionary, one by one:

Try it Yourself »

Example

Print all values in the dictionary, one by one:

Try it Yourself »

Example

You can also use the values() method to return values of a dictionary:

Try it Yourself »

Example

You can use the keys() method to return the keys of a dictionary:

Try it Yourself »

Example

Loop through both keys and values, by using the items() method:

Try it Yourself »

Python – Copy Dictionaries


Copy a Dictionary

You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only be a reference to dict1, and changes made in dict1 will automatically also be made in dict2.

There are ways to make a copy, one way is to use the built-in Dictionary method copy().

Example

Make a copy of a dictionary with the copy() method:

Try it Yourself »

Another way to make a copy is to use the built-in function dict().

Example

Make a copy of a dictionary with the dict() function:

Try it Yourself »

Python – Nested Dictionaries


Nested Dictionaries

A dictionary can contain dictionaries, this is called nested dictionaries.

Example

Create a dictionary that contain three dictionaries:

Try it Yourself »

Or, if you want to add three dictionaries into a new dictionary:

Example

Create three dictionaries, then create one dictionary that will contain the other three dictionaries:

Try it Yourself »

Python Dictionary Methods


Dictionary Methods

Python has a set of built-in methods that you can use on dictionaries.

Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
fromkeys() Returns a dictionary with the specified keys and value
get() Returns the value of the specified key
items() Returns a list containing a tuple for each key value pair
keys() Returns a list containing the dictionary’s keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
setdefault() Returns the value of the specified key. If the key does not exist: insert the key, with the specified value
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary

Python Dictionary Exercises


Test Yourself With Exercises

Now you have learned a lot about dictionaries, and how to use them in Python.

Are you ready for a test?

Try to insert the missing part to make the code work as expected:


Test Yourself With Exercises

Exercise:

Use the get method to print the value of the “model” key of the car dictio

Start the Exercise

Go to the Exercise section and test all of our Python Dictionary Exercises:

Python Dictionary Exercises

ArmenianEnglish