The Python dic­tion­ary 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­vent­or­ies. The Python dic­tion­ary 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­ment­a­tion

What is a Python dic­tion­ary?

A Python dic­tion­ary 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­tion­ary where a term is as­so­ci­ated with an ex­plan­a­tion or trans­la­tion, the Python dic­tion­ary creates an as­so­ci­ation table which cannot contain du­plic­ates. This means that each key can only occur once.

The structure of a Python dic­tion­ary

A Python dic­tion­ary is always in curly brackets. The key and its cor­res­pond­ing value are connected by a colon, and the resulting pairs are separated by commas. The­or­et­ic­ally, a Python dic­tion­ary 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­tion­ary is read by placing the correct key in square brackets. Below you will find how it is written in code:

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

Python dic­tion­ary example

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

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

Use the square brackets to query a specific element:

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

The output would be the value as­so­ci­ated with the key `France´, which would be `Paris´.

Al­tern­at­ive structure

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

capitals = { }

Complete the Dic­tion­ary:

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

When you query the Python dic­tion­ary `capitals´, the following will be outputted:

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

How do I change the Python dic­tion­ary?

You can also modify your Python dic­tion­ary 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­tion­ary?

There are three options available to sub­sequently remove a key-value pair from the Python dic­tion­ary: 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­res­pond­ing Python dic­tion­ary 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­tion­ary will be deleted. This is a Python problem which occurs re­l­at­ively often.

Remove with pop

The second way to remove key-value pairs from a Python dic­tion­ary 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­tion­ary. 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­tion­ary

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

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