Python Dictionary Tutorial with Examples

Dictionary represents the key-value pair in Python, enclosed in curly braces. Keys are unique and a colon separates it from value, whereas comma separates the items. The keys are immutable (strings, numbers, etc.), whereas the value can be of any Python object. Apart from this, Keys in Dictionary are case-sensitive.

Note: You can easily relate Python Dictionaries with the real-world Dictionary, such as a word with its meaning as key-value pair.

How to create Python Dictionary

To create a Dictionary, set items separated by comma. By items, we meant the key-value pair, with a key and its value, for example:

Above, we have created a Dictionary object mystock. Moving further, on the left side before colon (:) is the key, whereas on the right, the corresponding value. Key can be a number, tuple or string. We have string as our Key. Furthermore, we have 3 key-value pairs inside the curly braces separated by comma. Let us now see the representation before moving further with a live example.

The mystock is our Dictionary object. The ”Product”, “Price” and “Quantity” are the string, which is an immutable object. Remember, the key cannot be repeated. The “Product”: “Earphone” is one of the 3 key-value pairs in the above example:

Python Dictionary

Note: In the below examples, we have taken a Dictionary object mystock, and set Product details as key-value pairs.

Let us now see an example to create a Dictionary in Python with string keys:

The output is as follows:

Following is the representation of the above example:

Python Dictionary with 4 key value pairs

Let us now see another example wherein we will create Dictionary using the dict() method:

The output is as follows:

Let us now see how to create an empty Dictionary in Python:

The output is as follows:

How to access values in Python Dictionary

To access values in Dictionary, use the key name. Therefore, refer the key under square brackets and fetch the corresponding value, for example:

Above, will fetch the corresponding value of the key “Product”.

Let us now see an example and fetch specific values from keys:

The output is as follows:

In the above code we displayed the corresponding values for key “Product” and “Quantity“. You can achieve the same result with the get() method as in the below example:

As shown above, we are fetching the corresponding value for key “Product” using the get() method. Let us now see an example to fetch corresponding value using get():

The output is as follows:

Print all the keys of a Dictionary

To print the keys, use the keys() method of the Python Dictionary. Let us see an example to display only the keys of a Dictionary in Python:

The output is as follows:

Print all the values of a Dictionary

To print the values, use the values() method of the Python Dictionary. Let us see an example to display only the values of a Dictionary in Python:

The output is as follows:

Update values in a Dictionary

In Python, using the key, we can update the values in Dictionary. Refer the key name for which you want to update the value as in the below example:

In the above code snippet, we have updated the corresponding value with key “Price” to 4000. Previously, it was 3000. Let us see an example wherein we have updated two values based on its keys:

The output is as follows:

Adding items to a Dictionary

To add items to an already created Dictionary, create a new index key and assign value to it, for example:

Let us now see an example and add items to an already created Dictionary:

The output is as follows:

How to delete an element in a Dictionary with a specific key

To delete specific elements in a Dictionary, use the del keyword and in the square bracket, set the key of specific element you want to delete. Let’s say you want to delete the element with key Price, then:

The above deletes the entry with key Price.

Let us now see an example and delete an element with a specific key:

The output is as follows:

Delete the Dictionary completely

To delete the Dictionary completely, use the del keyword in Python as in the below example:

The output is as follows:

Empty the Dictionary

To empty the Dictionary in Python, the clear() method is provided by Python. Let us see an example:

The code is as follows:

Above, using the clear(), empties the Dictionary. On printing the empty Dictionary, only two curly brackets are visible.

Delete keys from the Dictionary

To delete a key, use the del keyword in Python Dictionary as in the below example:

The output is as follows:

Delete a key and return the corresponding value

To delete a key and return the corresponding value, use the pop() method. Let us see an example wherein we will be deleting a key and return the corresponding value:

The output is as follows:

Iterate through a Dictionary

Use the for loop in Python to iterate through a Dictionary as in the below example:

The output is as follows:

Display a printable string representation of a Dictionary

To display a printable string representation of a Dictionary on Python, the str() method is used. Let us see an example:

The output is as follows:

Get the length of the Dictionary

To get the total length of the Dictionary, the len() method is used as in the below example:

The output is as follows:

Make a copy of a Dictionary

Use the copy() method in Python Dictionary to make a copy, for example, a copy of mystock Dictionary will get created like this:

Let us now see an example to make a copy of a Dictionary:

The output is as follows:

Create a Multi-dimensional Dictionary

To create a multi-dimensional Dictionary, at first create Dictionary objects.

After that, create a multi-dimensional Dictionary and set the dictionaries as their value:

Let us now see an example:

The output is as follows:

Properties of Dictionary keys

Following defines what Dictionary keys rules are:

  • Keys are unique and cannot be repeated.
  • Dictionary Keys are case-sensitive.
  • Keys are immutable.
  • Key can be a number, tuple or string.

Create Nested Dictionaries

A nested Dictionary is a Dictionary that has many Dictionaries. Let us see an example, wherein we will create a Dictionary that has 2 Dictionaries:

The output is as follows:

Let us now see an example of a Dictionary with 4 Dictionaries i.e. Nested Dictionary:

The output is as follows;

Access elements from a Nested Dictionary

To access elements from a Nested Dictionary, use the square brackets:

Above code snippet fetches the value of key Product from Dictionary2 i.e. mystock2. Let us now see an example with 4 Dictionaries i.e. Nested Dictionary:

The output is as follows:

In this tutorial, we learned about Python Dictionaries. Moreover, we saw how to create a Dictionary and perform operations on it with live running examples. Additionally, some illustration were also used to explain the concept thoroughly.

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Tuples Tutorial with Examples
Python Lists with Examples
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading