×

Html & Html5

The World's best Software Development Study Portal

Python Dictionary


Dictionary is that collection data type of Python which is unordered , changeable and indexed and these are also written by curly brackets {} , having keys and its values .
Examples:-


Create a dictionary


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

""" keys here are fruits , Vegetables & Color whereas Apple, Carrot and Red are values of this keys """

print(thisdict)




Output of this Program


So here in above program we had given the command of creating dictionary data to our Python interpreter. Let’s see .




dictionary1

So our ‘Dictionary’ is been ready.




Access Items in dictionary


Here in the Dictionary data type ,we can access keys in dictionary by referring to the key value inside square bracket[] .

Let’s understand this by an example :-


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

print(thisdict["Vegetable"])




Output of this Program


So here in the above program we just want to access the value ‘Carrot’ which is the value of ‘Vegetable’ . So we just type down the ‘Vegetable’ key to access ‘Carrot’ .
So let’s see what output it gives :-




dictionary2

So we just get the ‘Carrot’ value which we want just by passing the key.




Change dictionary values


We can change the dictionary items just by referencing the keys and giving it the desired value.
Let’s understand this by the following example :-


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

thisdict["Fruits"]= "Banana"

print(thisdict)





Output of this Program


So here in the above Program we just gave command to our Python interpreter that we want to change the value of ‘Fruits’ from ‘Apple’ to ‘Banana’ .
So let’s see what output it gives :-




dictionary3

So here it just changes the ‘Apple’ to ‘Banana’




Loops through dictionary


Unlike other collection data type here also loops is done by using ‘for’ loop but only the keys values we can get in return , although there are methods too how we can get the values in return .

Let’s understand this by an example :-


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

for X in thisdict:

print(X)




Output of this Program


So this is the method basically to give command to our Python interpreter that we want to print all the keys , one by one .
So let’s see what output it gives :-




dictionary4


Here we get all the keys present in our program , but if we want to get values in return than there is a different way

Let’s understand this by an example :-


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

for X in thisdict:

print(thisdict[X])




Output of this Program


So this is the method basically to give command to our Python interpreter that we want to print all the values , one by one .
So let’s see what output it gives :-




dictionary5


So here we get the values in return.




( More about Loops we will talk further on the looping section. )




Check if Key exist in dictionary


We can also check keys in our dictionary by using the ‘in’ keyword.
Let us understand this by the following example :-


thisdict={"Fruits": "Apple", "Vegetable": "Carrot", "Color" : "Red"}

if "Fruits" in thisdict:

print ("Yes it is in dictionary")




Output of this Program


dictionary6


So our ‘Fruits’ is in the dictionary and it gives us the correct output.


( We will read in detail regarding ‘if’ statement . )




Methods in Dictionary


As we know that methods are basically the function which we want to perform in our coding . In Python there are some inbuilt methods are there for dictionary too.
So let’s see some of them :-



The len() method - The len() method - It just returns the length of the Dictionary , considering key and its value as one .


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

print(len(thisdict))




Output of this Program


dictionary7


So here we just get the length of the set based on their index number.




Adding items on dictionary


Adding an element in dictionary we have to use new index key and assigning it a value.
Let’s understand this by the example :-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

thisdict["Number"]= 500

print(thisdict))




Output of this Program


dictionary8


So here through add() method we add new keys to our dictionary.




Removing item of dictionary


There are several types of method to removing the dictionary:-

  • pop() method
  • popitem() method
  • del() method
  • clear() method

the pop method remove any item or key with the specified key name.
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

thisdict.pop("Fruits")

print(thisdict)




Output of this Program


dictionary9


Here we had just removed the ‘Fruits’ key plus its value too from our dictionary by pop() method.




the popitem() method removes the last element or key of our dictionary.
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

thisdict.popitem()

print(thisdict)




Output of this Program


dictionary10


So here our last key and its value is vanish just by using popitem() method .




the del() keyword removes the item specified key name.
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

del thisdict["Fruits"]

print(thisdict)




Output of this Program


dictionary11


So here we just deleted the key ‘Fruits’ and it value too.




del() keyword removes the whole dictionary too.
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

del thisdict

print(thisdict)




Output of this Program


dictionary12


So here we just deleted the whole dictionary.




clear () keyword empties the dictionary.
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

thisdict.clear()

print(thisdict)




>

Output of this Program


dictionary13


Here whole dictionary is empty or invisible by using clear() method.




Copy a dictionary


We can copy the dictionary by giving references , and there are two methods of copying the dictionary.

  • copy ()
  • dict()


copy the dictionary through copy () methods .
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

mydict=thisdict.copy()

print(thisdict)




Output of this Program


dictionary14


Here we have copied thisdict keys and values into mydict, and when we print mydict , it give us all the elements which are present in thisdict




copy the dictionary through dict() methods .
example:-


thisdict = {"Fruits": "Apple", "Vegetable": "Carrot", "Color": "Red"}

mydict=dict(thisdict)

print(thisdict)



Output of this Program


dictionary15


So through dict() method we copied our thisdict library to mydict.




The dict() constructor , dict() can used to make new dictionary.


thisdict =dict(Fruits= "Apple", Vegetable= "Carrot", Color= "Red") keywords are not string literals
# use of equals rather than colon for the assignment

print(thisdict)



Output of this Program



dictionary16


So here through set() constructor our set is ready.




Some methods of dictionary and their functionality



Python has set of built-in method that you can use the dictionary



Method Description
clear() Remove all element to dictionary
copy() Return copy on the dictionary
fromkeys() Create dictionary from given sequence
get() Return values of the keys
items() Returns view list of dictionary (key.values) pair
key() Return the containing keys list of dictionary
pops() Remove the element with specified keys
popitem () Remove the last inserted key-value pair
setdefault() Insert key with values if key not present
update() Updates the dictionary
values() Returns all view values in dictionary


Check all these methods for good practice.



python training insitute| Best IT Training classes in Gurgaon | python training