Operations on Dictionary in Python

Operations on Dictionary in Python

Below is declaration of Dictionary on which we shall explain some of the operations that can performed:

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

-----------------------------------------------

Accessing the elements of Dictionary

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

#Items in dcitionary are accessed using keys
print(name[2])

The output is:
Embeddeddesignblog

-----------------------------------------------

Finding the length of Dictionary

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

print(len(name))

The output is 

as there are 4 items in the dictionary

-----------------------------------------------

Finding the maximum value of the dictionary

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

#Maximum shall be determined from the key and not items
print(max(name))

The above print output 
as 4 is the maximum key number in the dictionary

-----------------------------------------------

Finding the minimum value of the dictionary

#Maximum shall be determined from the key and not items
print(min(name))

#The above print output 
as 1 is the minimum key number in the dictionary

-----------------------------------------------

Updating the item using key

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

name[1] = "forum.talenteve.com"
print(name)

#The output is now
{1: 'forum.talenteve.com', 2: 'Embeddeddesignblog', 3: 'YouTube-Way2Know', 4: 'Myvision-thisworld'}

-----------------------------------------------

Adding new item to dictionary

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

#Adding new item to dictionary
name[5] = "forum.talenteve.com"
print(name)

#The output is
{1: 'TalentEve', 2: 'Embeddeddesignblog', 3: 'YouTube-Way2Know', 4: 'Myvision-thisworld', 5: 'forum.talenteve.com'}

-----------------------------------------------

Converting Dictionary to string

{1: 'TalentEve', 2: 'Embeddeddesignblog', 3: 'YouTube-Way2Know', 4: 'Myvision-thisworld', 5: 'forum.talenteve.com'}

#converting dictionary to string
print(str(name))

#The output shall be string
{1: 'TalentEve', 2: 'Embeddeddesignblog', 3: 'YouTube-Way2Know', 4: 'Myvision-thisworld', 5: 'forum.talenteve.com'}

-----------------------------------------------

Deleting an element in Dictionary

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld"}

#deleting an element in dictionary
del name[1]
print(name)

#The output from above print shall be:
{2: 'Embeddeddesignblog', 3: 'YouTube-Way2Know', 4: 'Myvision-thisworld'}

-----------------------------------------------

Deleting dictionary

#deleting entire dictionary
del name
print(name)

#The output from above print shall be:
NameError: name 'name' is not defined

#NameError is output as the dictionary is deleted

-----------------------------------------------

Clearing a Dictionary

Contents of a dictionary can be cleared using the clear() function

name = {1:"TalentEve",2:"Embeddeddesignblog",3:"YouTube-Way2Know",4:"Myvision-thisworld",5:"TalentEve"}

print(name.clear())

The output of above code shall be:
None

It is None as there are no contents in Dictionary

Post a Comment

0 Comments