Dictionary

Dictionaries are defined by the keyword {}. It is used to collect the data and store in a sequence.

>>> data = { ‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ }

>>> data[Name]

Chandru

We can add element in the dictionary by

>>> data[‘Age’] = 18

{‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ , ‘Age’ : 18}

To delete the element in the dictionary

>>> del data[‘Age’]

{‘Name’ : ‘Chandru’ , ‘Gender’ : ‘Male’ }

To check whether the given element is present or not

>>> Arun in data

False

To combine two sets, the keyword dict is used

>>> dict(((‘India’,’America’) , (‘Chennai’,’Losangels’)))

{‘India’ : ‘Chennai’ , ‘America’ : ‘Losangels’}

To append the values in dictionary

>>> data = {}

>>> data.setdefault(‘Name’, []).append(‘Chandru’)

>>> data

{‘Name’ : [‘Chandru’]}

To print the values in dictionary

>>> for x,y in data.items():

… print(“%s is %s” % (x,y)

Name is Chandru

Gender is Male

Leave a comment