Python Dictionary

In Python, a dictionary is an unordered collection of items, with each item consisting of a key: value pair (separated by a colon).

Create a Dictionary

You can create a dictionary by enclosing comma separated key: value pairs within curly braces {}. Like this:

Here's an example of creating a dictionary, then printing it out, along with its type:

Result
{'Earth': 40075, 'Saturn': 378675, 'Jupiter': 439264}
<class 'dict'>

But that's not the only way to create a dictionary. There's also a dict() function for creating dictionaries. And you can also use syntax variations within that function. Here are some examples:

Dictionary containing Lists & Tuples

Dictionaries can also contain lists and tuples. Example:

Result
{'Fruit': ['Apple', 'Orange', 'Banana'], 'Vegetables': ('Eat', 'Your', 'Greens')}

Access the Values in a Dictionary

There are two ways to access the values in a dictionary; the square bracket notation, and the get() function.

With both methods, you access an item by referring to its key. This is slightly different to accessing items within a list or tuple (where you access an item by referring to its index).

The key can be enclosed in square brackets, or passed as an argument to the get() function.

Here's an example using both methods:

Result
40075
378675

Dealing with Missing Keys

One of the differences between the two notations is what they do if you supply a key that doesn't exist.

If you reference a non-existent key, get() returns None, whereas the square brackets notation will raise a KeyError.

But the get() function also allows you to provide a default value in the event the key doesn't exist. You can provide this as a second parameter to the function.

Here's an example where we use the get() function twice, but we only provide a default value for the second one.

Result
None
Huh?

Access a List Item within a Dictionary

You can access a list item within a dictionary by referring to the list's key within the dictionary, followed by the list item's index within the list (each index surrounded by its own set of square brackets).

Like this:

Result
Orange

Update a Dictionary

Dictionaries are mutable, so that means you can update items within the dictionary.

You can update the dictionary by referring to the item's key. If the key exists, it will update its value to the new value. If the key doesn't exist, it will create a new key:value pair.

Update an Existing Item

Here's an example that demonstrates updating an existing item:

Result
{'Name': 'Christine', 'Age': 23}
{'Name': 'Christine', 'Age': 24}

Add a New Item

Here's an example that demonstrates adding a new item (i.e. updating a key that doesn't exist):

Result
{'Name': 'Christine', 'Age': 23}
{'Name': 'Christine', 'Age': 23, 'Height': 154}

Update a List Item within a Dictionary

If your dictionary contains lists, you can update list items within those lists by referring to the list's key within the dictionary, followed by the list item's index within the list. Here's an example:

Result
{'Fruit': ['Apple', 'Orange', 'Banana']}
{'Fruit': ['Apple', 'Orange', 'Watermelon']}

Delete a Dictionary Item

You can use the del keyword to delete a given dictionary item. To do this, simply refer to the item's key. Like this:

Result
{'Earth': 40075, 'Saturn': 378675, 'Jupiter': 439264}
{'Earth': 40075, 'Saturn': 378675}

Delete a Whole Dictionary

You can use the del keyword to delete the whole dictionary. Like this: