Dictionary in Python
Our topic is to understand how almost a dictionary works in Python.
A Dictionary is an unordered collection of elements.
Moreover, they are mutable and indexed by keys.
You may learn: What are the Mutable and Immutable objects in Python?
Creation, Addition, Removal and Modification of Dictionary in Python
Creating a Dictionary in Python
dictionary = { # dictionary created with curly braces
"key" : "value" ,
"student_num" : 1
}
print (dictionary)Output:
{'key': 'value', 'student_num': 1}
However, After creating this sample dictionary there are two things to consider.
- Key: The values in Dictionary are accessed through keys.
- Value: Value is the information or the data.
Also learn: Creating A Dictionary From A String In Python
After that, here is an example to show how variables can be used as keys.
Certainly, the elements in the dictionary are in form of key: value pair.
The keys in the dictionary should be immutable (which cannot be modified).
After that, it is important to have values in the dictionary by the user’s choice, so
Create a Dictionary with dynamic inputs:
dictionary = { } # dictionary created with curly braces
n = int(input()) # number of inputs in dictionary
for i in range (n):
key = input() # Enter the Key
value = input() # Enter the Value
dictionary[key] = value # Appending to Dictionary
print (dictionary)Output :
3
roll _no
34
pin_no
567
name
rahul
{'pin_no': '567', 'roll_no': '34', 'name': 'rahul'}
Moreover, the output sequence can be shuffled as Dictionary is an unordered sequence.
Dictionary in Python with in-built Functions:
d1 = { # dictionary d1 created with curly braces
'1' : 'Bhanu' ,
'2' : 'Surya' ,
'3' : 'vivek' ,
'4' : 'Rohit' ,
'5' : { '6' : [1,2,3] } # created nested dictionary
}
print ("Initial dictionary")
print (d1)
print("\n")
print ("Deleting item using key")
print (d1.pop('2')) # deleting the key '2' using .pop( key ) method
print("\n")
print ("copy to other")
d2 = d1.copy() # copies d1 to d2
print (d2)
print("\n")
print ("Keys in the dictionary:")
print (d1.keys()) # list of keys in dictionaries
print("\n")
print("Items in the dictionary:")
print (d1.items()) # tuples containing keys and values
print("\n")
print ("Pops out any arbitary pair")
print (d1.popitem()) # removes arbitary pair of key : value in a dictionary
print ("\n")
print("After clearing items in the dictionary")
print (d1.clear()) # clears all the elements in d1
print ("\n")
print ("Deleting the dictionary itself")
del d1 # deletes the whole dictionary
Output :
Initial dictionary
{'5': {'6': [1, 2, 3]}, '4': 'Rohit', '2': 'Surya', '1': 'Bhanu', '3': 'vivek'}
Deleting item using key
Surya
copy to other
{'5': {'6': [1, 2, 3]}, '4': 'Rohit', '3': 'vivek', '1': 'Bhanu'}
Keys in the dictionary:
dict_keys(['5', '4', '1', '3'])
Items in the dictionary:
dict_items([('5', {'6': [1, 2, 3]}), ('4', 'Rohit'), ('1', 'Bhanu'), ('3', 'vivek')])
Pops out any arbitary pair
('5', {'6': [1, 2, 3]})
After clearing items in the dictionary
None
Deleting the dictionary itselfNow, Certainly, the idea of using the dictionary becomes very clear with useful inbuilt functions.
Concluding this part, there are certainly some key points to note down.
- The keys in the dictionary should be immutable.
- There should be no duplicates in the dictionary.
- Keys in a dictionary should avoid polymorphism.
- Dictionary seems like a normal sequence like lists but is accessed by keys instead of index.
Leave a Reply