Dictionaries in Python and it’s Example

0
276
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.

Old_Dictionary_name.update(new_dictionary_name)

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.

dict1.update(dict2)

Here we update the dict1 with dict2.

Output:

{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.

Old_Dictionary_Name[key]=value 

For example:

Dict1[5]= ‘e’

Here we add the item in the predefined dictionary.

Output:

{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:

dict1
o/p
{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.

Syntax:

variable_name.values()

For example:

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

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

dict3.values()
o/p
‘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}
Dict2[‘c’]
o/p
3

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.

Syntax:

variable_name/dictioanry_name.keys()

For example:

dict3 = {'N': 'Nagpur', 'M': 'Mumbai'}
dict3.keys()
o/p
dict_keys([‘N’,’M’])

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.

Syntax:

variable.items()

For example:

dict3.items() 
 dict_items([('N', 'Nagpur'), ('M', 'Mumbai')])

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

Conclusion

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here