In this article, we will discuss dictionaries in python and will try to answer questions like,

  • What is a dictionary?
  • Why do we need it?
  • How to create a dictionary?
  • How to access elements in a dictionary?

What is a dictionary in python?

In python, a dictionary is a kind of container that stores the items in key-value pairs, like,

This is an example of dictionary, which contain student names as keys and their age as values. Elements in the dictionary are stored as key-value pairs, where each value is mapped with a key. It is also known as associative array or hash table.

The above dictionary contains four items, i.e. four key-value pairs,

  1. Jack & 32
  2. Ritika & 31
  3. Mark & 22
  4. Mathew & 27

Why do we need dictionaries?

As a dictionary, keeps the elements in key-value mapping format and internally uses hashing for it; therefore, we can get a value from the dictionary by its key very quickly. In best cases, its complexity is O(1), whereas, in the worst case, its complexity can be O(n).

If you want to know more about hashing check this article –> What is Hashing and Hash Table?

How to create a dictionary?

To create a dictionary, we can use curly braces, i.e., {}.

It will create an empty dictionary.

Now to create a dictionary with items, pass the key-value pairs in curly braces  {}. Also, in every couple, key & value will be separated by a colon (:).

Let’s understand by some examples,

Create a dictionary that contains student names as keys and their age as value i.e.

This dictionary contains four items. We can print the dictionary to check it contents,

Output:

Essential points about keys in the dictionary

  • Keys are always unique in the dictionary
  • keys must be of an immutable data type, i.e., strings, numbers, or tuples.
    • It means once a key-value pair is added in the dictionary then it cannot modify the key itself, although we can change the value associated with it.

For example, if we create a dictionary with duplicate keys and print its content,

Output:

The value of jack is overwritten because keys are always unique in a dictionary.

Important points about value in dictionaries.

The values in a dictionary can be of any type.

For example, let’s create a dictionary where key is integer and value is a list of strings i.e.

Similarly, a dictionary can have any object as the value.

How to access an item in the dictionary

Suppose we have a dictionary like this,

We can access a specific item/pair in a dictionary using [] operator on the dictionary object. If we call the operator [] on the dictionary object and pass a key, then it will return its value is like this,

Output:

Now if we pass a key that doesn’t exist in the dictionary, then it returns a KeyError,

Error

So, we before accessing any value in a dictionary, we should first check if given keys exist in the dictionary or not i.e.

Learn more about dictionaries like how to add, modify, remove contents in a dictionary and many other frequently asked questions.

Python : 6 Different ways to create Dictionaries

Different ways to Iterate / Loop over a Dictionary in Python

Python : How to check if a key exists in dictionary ?

Python : How to add / append key value pairs in dictionary using dict.update()

Python: 4 ways to print items of a dictionary line by line

Different ways to Remove a key from Dictionary in Python | del vs dict.pop()

Python : How to Sort a Dictionary by key or Value ?

The complete example is as follows,

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,