Python – Dictionary
As we read earlier about Python Tuples , Now we will study about Dictionary in python. In the python programming language, this is our third collection of data type. In this, we will cover python dict, create a dictionary python , how to use dictionary in python, python key value, python dict example etc. so let’s start reading about the second collection data type.
Dictionary in python
Dictionary is a collection or sequence of key – values pair type of elements. Dictionary is mutable datatype i.e. they can be edited or modified.
In python programming dictionaries are written with Curly Brackets.
Python dict syntax :
dictionary variable = { comma separated element }
Note : Element is a pair of key and values. where,
key = Any immutable datatype
value = Any datatype
Python dict example :
key value key value key value key value ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ dict_1 = {'a' : 1, 'b' : 22, 'c' : 333, 'd' : 4444} print(dict_1)
Output :
{'a': 1, 'b': 22, 'c': 333, 'd': 4444}
How to access the items from a dictionary ?
We can access the dictionary items by using its key name.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
print(dict_1['c'])
Output :
333
CRUD for dictionary
CRUD Operation are those basic and important operation which one should know how to apply after studying “Dictionary” data type.
C – CREATE
Our first operation of CRUD which is “C” for create which means either to add elements or to insert them.
How to add items in a dictionary ?
We can add items into dictionary by using the new index key and assigning a value in it.
Syntax for add items :
dict_1[key] = value
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
dict_1['e'] = 55555
print(dict_1)
Output :
{'a': 1, 'b': 22, 'c': 333, 'd': 4444, 'e': 55555}
R – READ
Our second operation of CRUD which is ” R ” for read which means either to read data or to search data from the dictionary.
How to access items from a dictionary ?
This is an alternative method to access the dictionary items by using the get( ) function.
Syntax for access items :
variable = dict_1.get( key )
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
x = dict_1.get("c")
print(x)
Output :
333
U – UPDATE
Our third operation of CRUD which is “U” for update operation is used to modify the contents of a list or to make changes in a dictionary.
How to change / update the values ?
We can change / update the value of a specific item by using its key name.
General syntax for update values :
dictionary_variable[key] = value
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
dict_1['b']=22222
print(dict_1)
Output :
{'a': 1, 'b': 22222, 'c': 333, 'd': 4444}
There is an alternative to update the dictionary values.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
dict_2 = {'e':55555}
dict_1.update(dict_2)
print(dict_1)
Output :
{'a': 1, 'b': 22, 'c': 333, 'd': 4444, 'e': 55555}
D – DELETE
Our last operation of CRUD which is “D” for delete operation is used to remove an element from the dictionary.
How to remove the items ?
We can remove the items by using the pop( ) function with the specified key name.
General syntax for removing items :
dictionary_variable.pop( key )
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
dict_1.pop('c')
print(dict_1)
Output :
{'a': 1, 'b': 22, 'd': 4444}
Python Loop through dictionary
In dictionary datatype, we can use only element loop. There are some element loops which is used in dictionary are:
We can print all the key names one by one which is given in the dictionary.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
for e in dict_1:
print(e)
Output :
a
b
c
d
The above loop always return keys of the dictionary.
We can print all the key names one by one which is given in the dictionary with the different loop.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
for e in dict_1.keys():
print(e)
Output :
a
b
c
d
This loop always return keys of the dictionary.
We can print all the values one by one which is given in the dictionary.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
for e in dict_1.values():
print(e)
Output :
1
22
333
4444
This loop always return values of the dictionary.
We can print both keys and values which is given in the dictionary.
Example :
dict_1 = {'a':1, 'b':22, 'c':333, 'd':4444}
for e in dict_1.items():
print(e)
Output :
('a', 1)
('b', 22)
('c', 333)
('d', 4444)
This loop always return tuple datatype.
Nested dictionary Python
Nested dictionary means putting a dictionary inside another dictionary is called Nested dictionary. In other words, python dictionary inside dictionary.
Example :
dict_1 = {'A': {'a':1}, 'B':{'b':22}}
print(dict_1)
Output :
{'A': {'a': 1}, 'B': {'b': 22}}
Python dict( ) constructor
This is an alternative method to create a dictionary by using dict( ) constructor.
Example :
dict_1 = dict(a = 1, b = 2, c = 3)
print(dict_1)
Output :
{'a': 1, 'b': 2, 'c': 3}
Dictionary Python Functions
1. len( ) : To check how many items a dictionary has, by using the len( ) function.
2. clear( ) : It removes all the elements from the given dictionary.
3. copy( ) : Returns a copy of the dictionary.
4. del( ) : The del keyword removes the item with the specified key name and also del python dictionary completely.
«Previous Next» >