Monday, February 3, 2020

Dictionaries

Dictionaries

We can store key-value pairs with the help of a dictionary. We can store and retrieve values with the help of keys in a dictionary. There are many ways to initialize a Dictionary. Dictionaries are changeable and indexed. As the values are referenced by key, hence they can be accessed very fast, However, Dictionaries are unordered.

Curly brackets are used to create a dictionary, for example

mydict1={‘key’,’value’} #initialized dictonary
mydict2={} #Empty dictonary





Example
Example
We can create a dictionary with the help of dict() constructor. For example
mydict=dict(1='python',2='Java',3='C',4='Android',5='node.js')

We can use copy() method to copy the complete dictionary to another and clear() method to remove all the elements of a dictionary. del keyword can be used to remove or completely delete the Dictionary.



Nested Dictionary
We can also create nested dictionaries. It means we can put other/multiple dictionaries inside a dictionary.