The Python dic­tio­nary is an important data type in Python’s internet pro­gram­ming language. It is used to link terms and meanings together. Key-value pairs are used to create tables, data col­lec­tions, or in­ven­to­ries. The Python dic­tio­nary can also be used with Python “for” loops, Python “if else” state­ments, and Python “while” loops.

IONOS Developer API
Manage your hosting products through our powerful API
  • DNS man­age­ment
  • Easy SSL admin
  • API doc­u­men­ta­tion

What is a Python dic­tio­nary?

A Python dic­tio­nary is a data type which links terms and meanings. This structure allows keys and values to be linked, stored, and later retrieved. Similar to a regular dic­tio­nary where a term is as­so­ci­at­ed with an ex­pla­na­tion or trans­la­tion, the Python dic­tio­nary creates an as­so­ci­a­tion table which cannot contain du­pli­cates. This means that each key can only occur once.

The structure of a Python dic­tio­nary

A Python dic­tio­nary is always in curly braces. The key and its cor­re­spond­ing value are connected by a colon, and the resulting pairs are separated by commas. The­o­ret­i­cal­ly, a Python dic­tio­nary may contain any number of key-value pairs. The key is enclosed in quotation marks. You probably already know how to write it from our Python tutorial. See the example below:

ages = {'Jim': 42, 'Jack': 13, 'John': 69}

The Python dic­tio­nary is read by placing the correct key in square braces. Below you will find how it is written in code:

age_jim = ages['Jim']
assert age_jim == 42

Python dic­tio­nary example

The Python dic­tio­nary’s structure and func­tion­al­i­ty can be explained with a simple example. Countries and capitals are assigned and outputted in the following structure. The Python dic­tio­nary should look like this:

capitals = { "UK": "London", "FR": "Paris", "DE": "Berlin" }

Use the square braces to query a specific element:

capitals["FR"]
assert capitals["FR"] == 'Paris'

The output would be the value as­so­ci­at­ed with the key “France”, which would be “Paris”.

Al­ter­na­tive structure

You can also build a Python dic­tio­nary without content and fill it af­ter­wards. An empty Python dic­tio­nary is always created first for this and it should look like this:

capitals = { }

Complete the Dic­tio­nary:

capitals["UK"] = "London"
capitals["FR"] = "Paris"
capitals["DE"] = "Berlin"

When you query the Python dic­tio­nary “capitals”, the following will be outputted:

{ "UK": "London", "FR": "Paris", "DE": "Berlin" }

How do I change the Python dic­tio­nary?

You can also modify your Python dic­tio­nary later on. Simply write the following to attach key-value pairs:

capitals[ "IT" ] = "Roma"
print(capitals)

It should now look like this:

{ "UK": "London", "FR": "Paris", "DE": "Berlin", "IT": "Roma" }

Use the following code to change a value within a key-value pair:

capitals = { "UK": "London", "FR": "Paris", "DE": "Berlin", "IT": "???" }
assert capitals["IT"] == "???"
capitals["IT"] = "Roma"
assert capitals["IT"] == "Roma"

The output then reads:

{ "UK": "London", "FR": "Paris", "DE": "Berlin", "IT": "Roma" }

How do I remove key-value pairs from the Python dic­tio­nary?

There are three options available to sub­se­quent­ly remove a key-value pair from the Python dic­tio­nary: del, pop, and popitem.

Remove with del

A simple inventory is a good example of the del method. Let’s assume that a bakery offers 100 rolls, 25 loaves of bread, and 20 crois­sants when it opens for business. The cor­re­spond­ing Python dic­tio­nary would look like this:

stock = { "rolls": 100, "breads": 25, "croissants": 20 }

If the bakery has sold all its crois­sants and wants to delete them from its listing, the del command can be used to do this:

del stock["croissants"]
print(stock)

You must make sure that you always use the del command in con­junc­tion with a key-value pair with this method. Otherwise, the entire Python dic­tio­nary will be deleted. This is a Python problem which occurs rel­a­tive­ly often.

Remove with pop

The second way to remove key-value pairs from a Python dic­tio­nary is pop. This stores the removed value in a new variable. It works as follows:

marbles = { "red": 13, "green": 19, "blue": 7, "yellow": 21}
red_marbles = marbles.pop("red")
print(f"We've removed {red_marbles} red marbles.")
print(f"Remaining marbles are: {marbles}")

The output will look like this:

We've removed 13 red marbles.
Remaining marbles are: {'green': 19, 'blue': 7, 'yellow': 21}

Remove with popitem

Use popitem to remove the last key-value pair from your Python dic­tio­nary. This is the code:

last = marbles.popitem()
print(last)
print(marbles)

Your output will look like this:

('yellow', 21)
{ "One": 1, "Two": 2, "Three": 3 }

Other methods for the Python dic­tio­nary

There are a few other methods that work with a Python dic­tio­nary. Below is a list of the most important options:

Method De­scrip­tion
Clear Clear removes all key-value pairs from the Python dic­tio­nary.
Copy Copy creates a copy of the dic­tio­nary in storage.
Get Get de­ter­mines the value by entering the key.
Keys Keys reads the keys of your dic­tio­nary.
Update Update extends a Python dic­tio­nary by one. Duplicate keys are over­writ­ten in the attached dic­tio­nary.
Values Values queries all your dic­tio­nary’s values.
Go to Main Menu