Monday, May 11, 2020

dictionaries in python

Inorder to implement data structure  in a simple manner we have dictionaries.

This is also a container, it can also store some objects.


What is the difference b/w sequences and dictionaries ?

Dictionaries is also a container, but this cannot be accessible through index.

and this is mutable, In dictionary you can modify the values.

Dictionary stores values in key-value pairs.


How to access the element in dictionary ?


x = { 'country' : 'In', 'Channel' : 'TWG'}
                                   key          value

Accessing Dictionaries:

print (x['Country'])  ---->  Prints "In" as output

it is through  'key', you can access it.


Example:










How to add new value  to the existing dictionary ?

x we can add the value to the existing dictionary.

Adding Key-Value Pair to existing Dictionary:

x['Mentor'] = 'Kaushik'


How to remove particular element from dictionary ?

removes the key value pair

del x['Country']


How to remove all elements ?

x.clear()    [ dictionary.clear () ]

you can clear method to remove all the elements.


How to delete the complete dictionary ?

del x

you can see curly braces in python that is only in dictionaries.

;   -------> optional 


Brief example,

























No comments:

Post a Comment