In this article , we will Learn Python Dictionary basics in simple way. Dictionary is a built-in Python Data Structure that is mutable and ordered. It Can’t have two items with same key. Dictionaries have key and value stored to it. Both key and value separated by semi colon ‘: ‘ .
1. Python Dictionary Creation
Creating a dictionary in Python is simple. You can create a dictionary by enclosing a set of key-value pairs in curly braces {}
or by using the built-in dict()
constructor. Let’s create a dictionary then :
var_dict={‘ClassTeacher’:’Prasad’ , ‘Student’:’Sam’, ‘Principle’ : ‘Rama’}
2. Accessing key value from Dictionary
If you want to get value of any key
var_dict[‘Student’]
If you want to pull all the keys in this dictionary
var_dict.keys()
Similarly, if you want to pull all the values in this dictionary.
var_dict.values()
if you want to pull both key and value together. it’s useful when you iterate over the dictionary and need both key and value to do any operation or write any logic.
var_dict.items()
4. Modify the Python Dictionary:
Like Python List , we can modify /update Python Dictionary . If you wanted to update any key , say Principle is changed and new Principle is Suraj .
var_dict[‘Principle’]=’Suraj’
More Resources :