Python Dictionary
A python dictionary is a collection which is unordered, changeable and indexed with No duplicate members.
How can we make a dictionary in python?
Basically there are two ways for defining a dictionary in python.
First- This is the most common way and it is generally used.
person = {
‘first_name’: ‘John’,
‘age’:22
}
Second-Using a constructor
person = dict((first_name=‘John’, age=22))
How to access the value of the dictionary?
The most general way to access the values of the dictionary is:-
person[‘first_name’]
Using get method to access the value.
person.get(‘first_name’)
Get dishanshu jagtap’s stories in your inbox
Join Medium for free to get updates from this writer.
How to add key/value in the dictionary?
person[‘phone’]=’444–4444–444'
How to Get Values ?
person.items()
How to Make a copy of a dictionary?
new_dict=person.copy()
How to Remove Items from the dictionaries?
del(person [‘age’])
How to Clear the dictionary?
person.clear()
The output of this will be an empty dictionary
How to Get Length?
len(person )
What is List of Dictionary?
list_of_dict=[
{‘name’:’dis’,’age’:22},
{‘company’:’Hungers’}
]
print(list_of_dict[0][‘age’])
Output- 22





