Dictionary in Python

by Monika Dadool 25-Jan-19

0 617


Dictionaries can be considered as the sequence of values containing key-value pairs i.e. to every key there are attached some values. They are written with curly braces and contained keys and values in it. Each key is separated by a colon (:) from its value.

Dictionaries are applicable when there are a huge amount of data to be managed and can be used in real time for key and their pair values storage. With the keys, values of the pairs can be accessed for modification, Deletion and extracted.

They are also known as the hashes because they use hashing operations for value storage. The values of the dictionaries will display though hash values. Since values of the dictionary are unordered they can be in sorted order with the sorted function.

Example
dictionary = {'website-name': 'codingtag', 'rank': 100, 'Class': 'grade 1'}
print ("dictionary['website-name']: ", dictionary['website-name'])
print ("dictionary['rank']: ", dictionary['rank'])

Output
dictionary['website-name']:  codingtag
dictionary['rank']:  100


How to add new item in existing Dictionary
dictionary = {'website-name': 'codingtag', 'rank': 100, 'Class': 'grade 1'}
dictionary['tutorials'] = "programming-languages"
print(dictionary)

Output
{'website-name': 'codingtag', 'rank': 100, 'Class': 'grade 1', 'tutorials': 'programming-languages'}


Deletion in Dictionaries
One can easily able to delete entire entries, or single entry and entire dictionary in python language with the help of del, clear() and with key value respectively.

Builtin methods in python
There are various builtin methods of dictionaries are present in python for making programmer's life easier i.e. clear(), get(), items(), key(), update(), values() and many more.


Share:


Comments

    Waiting for your comments

Leave a Reply