Dictionaries in Python and it’s Example

Dictionaries In Python

Introduction To Dictionaries in Python

A dictionaries in python is a data type that works with keys and values instead of indexes. Each value stored in a dictionary can be retrieved using a key, which is any type of object (a string, an integer, a list, etc.)

How To Create a Dictionary in Python

Dictionaries are always created with in a dictionary in { } brackets.
In a dictionary, we can pass the keys at the left side of the colon(:) and values at the right side of the colon(:).

Let’s check the syntax of the dictionary:

Variable_name = { key : value}

This syntax is used to create a single dictionary.

For example

Dictionary1={ ‘F’ : ‘Fireblaze’}

Here we pass the F as key and Fireblaze as a value.

Multiple Items in Dictionaries in Python

Let’s check the syntax to add multiple items in one dictionary

Variable name:-{key:value,key:value,etc}

This syntax is used to create multiple items inside the dictionary.

For example:

Dictionary2={‘D’: ‘Data’,’ S’:’ Science’}

In this Dictionary we pass 2 items.

Use of List In Dictionary

When you are performing with the dictionary and you want to add the list of values inside a single key.

Syntax to create a dictionary using list:

Variable name={key:[value1,value2,value3]}

Let’s check the example:

Dictionary3={‘Courses’:[’Data science’,’AI’,’ SQL’],2:[200,300,400,500]}

In this dictionary, we pass the list of values for the key courses.

Updating the Dictionary in Python

Update Function in the dictionary is used to update the predefined dictionary.

The syntax used to update the Dictionary.


For example:

dict1 = {1: 'a', 2: 'b'}

Here we create a dictionary which is having a key 1 and 2 whereas values are a, b
Now here we are going to create one more dictionary.

dict2= {3 : 'c', 4 : 'd'}

Here is the 2nd dictionary now let’s update the dict1 with dict2.


Here we update the dict1 with dict2.


{1: 'a', 2: 'b', 3: 'c', 4: 'd'}

Here all the value of dict2 is updated with the dict1.

The second method to update the dictionary. Add new items in the dictionary.

Syntax to add a new item in the predefined dictionary.


For example:

Dict1[5]= ‘e’

Here we add the item in the predefined dictionary.


{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e'}

Accessing the Dictionary in Python

To access the whole dictionary you have to pass the name of the variable in which you created the dictionary.

For the example:

dict1 = {1: 'a', 2: 'b'}
 dict2 = {'c': 3, 'd': 4}
 dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}

Here are three dictionaries and if I want to call the second dictionary’

Write in a cell the name of the dictionary is:

{1: 'a', 2: 'b'}

Access the values from the Dictionary in Python

To access all the values or to check the values which are present in the dictionary for that we have to pass the values function.



For example:

dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}

From this dictionary, we are going to retrieve the values using values function.

‘Nagpur’, Mumbai

These are the values that are present in the dictionary.

Access the particular value from the Dictionary.

Syntax to retrieve the particular value from the dictionary:

Dictionary_name/variable_name[name of the key]

For example:

dict2 = {'c': 3, 'd': 4}

Here we pass the c in square brackets the value of the c is 3 in the result.

Access the keys from the Dictionary.

To access the keys from the dictionaries in Python we have to use the keys function which is used to access all the keys from the dictionary.



For example:

dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}

Check the relationship between the key and value in the dictionary using items() function.

With the help of the items function, we get all the records and their key value relationship of dictionary in Python.



For example:

 dict_items([('N', 'Nagpur'), ('M', 'Mumbai')])

Here we access the information from the dictionary which key is related to which value.


In this blog, you will get all the information on the dictionary and the use in python.


Please enter your comment!
Please enter your name here