Skip to content

Python Dictionary – easily explained!

The Python Dictionary is used to store key-value pairs in a variable. It is one of a total of four data structures that are pre-installed in Python. In addition to the dictionary, these also include the tuple, the set, and the list.

We define a Python dictionary by writing the key-value pair in curly braces and separated by a colon. We can store elements with different data types in a dictionary.

Python Dictionary

We can query the elements of the dictionary by specifying the key in square brackets. Then we get the corresponding value stored for this key.

What are the basic features of a Python dictionary?

Since Python version 3.7 the dictionary is ordered. This means that the order in which we store the key-value pairs also plays a role. In the versions before that, in contrast, the order has no meaning. In addition, the Python dictionary is also modifiable, i.e. after it has been created, elements can be changed, added, or deleted from the dictionary.

The most important feature of the Python dictionary is that duplicate key-value pairs are not allowed. In other data formats in Python, however, duplicate elements are allowed. If we want to add a key-value pair to the dictionary whose key already exists, the old pair is overwritten without notification.

Python Dictionary

How to query elements?

There are various information and elements that we can query from a dictionary.

As we have already seen, we can query the value by defining the associated key in square brackets. Similarly, the “get()” method returns the same result:

Python Dictionary

With the commands “.keys()” and “.values()” Python returns us a list of all keys and values. The order of the lists also corresponds to how they are stored in the dictionary. This also means that the list of values may contain duplicates.

Python Dictionary

On the other hand, if we want to retrieve the full key-value pairs, we use the “.items()” method, which returns the pairs as a list of tuples:

Python Dictionary

How can elements of a dictionary be changed?

If we want to change individual values within the Python dictionary, we can do this directly via the key. Since there must not be duplicate keys, the old value is simply overwritten. If we want to change multiple pairs at once, we use the “.update()” method and define the new key-value pairs in it.

Python Dictionary

How to delete elements?

If we want to delete a single element from the Python dictionary, we can either specify the key and use the “pop()” method to specifically delete the element, or use “popitem()” to delete the last added key-value pair:

Python Dictionary

Finally, you can clear the entire Python dictionary with the “clear()” method:

Python Dictionary

Which Python collections are available?

In Python, there are a total of four data types that are stored by default:

  • The list is an ordered collection of elements, which is changeable and can also contain duplicate elements.
  • The tuple is in effect a list, with the difference that it is no longer changeable. So no elements can be added or removed afterward.
  • The set does not allow duplicate entries. At the same time, the arrangement of the elements within the set is variable. The set itself can be changed, but the individual elements cannot be changed afterward.
  • Since Python version 3.7, a dictionary is an ordered collection of elements that can be changed. In the earlier versions, the dictionary is unordered.

This is what you should take with you

  • The Python Dictionary is one of four pre-installed data structures in Python.
  • It is used to store key-value pairs in a single variable.
  • The values of a dictionary can have different values. Besides a single scalar, lists, tuples, or new dictionaries can also be stored as values.

Thanks to Deepnote for sponsoring this article! Deepnote offers me the possibility to embed Python code easily and quickly on this website and also to host the related notebooks in the cloud.

  • w3schools offers detailed examples of Python dictionaries with the possibility to execute code snippets directly online.
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Cookie Consent with Real Cookie Banner