5. What is Dictionary Data Type ?
Dictionary is an important concept of Python language, because dictionary is used to keep the data secure. Dictionary is a mutable data type, in which data is stored in the pair of key and value, that is, the created keys definitely have some value. Dictionary in Python language is written in curly brackets {}. In the code editor given below, student data has been stored in a variable using dictionary.
st = {
"Roll Number" : "1",
"Name" : "Shubham",
"Class" : "10th"
}
print(st)
{'Roll Number': '1', 'Name': 'Shubham', 'Class': '10th'}
st = { : A variable named st is created, in which the information of a student is stored
in the
dictionary.
print(st) : This print() function displays the information of the student stored in the
st
variable.
Dictionary Methods
Simply creating a dictionary, or storing data in it, is not enough. You must also know how to access and delete the dictionary. You must also know how to perform operations on the dictionary, such as delete, clear, add, update, etc. The most commonly used dictionary methods are described below.
1. Accessing Value (Method)
Once we create a dictionary, we need to access the dictionary keys. Accessing values ββfrom keys is very simple in Python. To access values ββfrom keys, functions like indexing or get() are used in Python. The values ββof a dictionary are accessed from keys in the code editor given below.
employee = {
"id" : "124"
}
print(employee["id"])
124
employee = { : A variable named employee is created, in which the data related to an
employee is
stored in the dictionary.
print(employee["id"]) : This print() function prints the value of the id key of the
employee table
on the screen.
2. Changing the value (Method)
Dictionary is an interesting concept of Python language, hence it does not happen that once the data is stored in the dictionary, changes cannot be made to the stored data. In Python language, the update method is used to change the values ββof the dictionary. In the code editor given below, information of a car has been stored in a dictionary and changes have been made to the price of the car using the update method.
car = {
"output": "10000"
}
car["prise"] = 20000
print(car)
20000
car = { : A variable named car has been created, in which the information of the car has
been
stored.
car["prise"] = 20000 : The price value of car has been set to "20000".
print(car) : This print() function displays the car variable.
3. Adding a new Key-Value (Method)
Dictionary is a mutable data type, so once the key and value are stored in the dictionary, new keys and values ββcan be added later if needed. Update method is used to add new key and value to the dictionary. The name of car has been added to the dictionary given below.
\car = {
"output": "10000"
}
car["Name"] = "sssss"
print(car)
{'prise': '10000', 'Name': 'sssss'}
car = { : A variable named car has been created, in which the information of car has been
stored in
the dictionary.
car["Name"] = "sssss" : New key values ββnamed "Name" and "sssss" have been added to the
dictionary
stored in the car variable.
print(car) : This print() function displays the car variable.
4. Deleting the Dictionary (Method)
"del" is already a reserved keyword in Python language, which is used to delete any value or element. In Python language, 'Del' keyword is used to delete a dictionary or the key values ββof a dictionary. Below, the key value of a dictionary has been deleted.
Student = {
"Roll Number" : "10"
}
del Student["Roll Number"]
print(Roll Number)
{'prise': '10000', 'Name': 'sssss'}
Student = { : A variable named Student is created, in which the information of a student
from the
dictionary is stored.
del Student["Roll Number"] : The "Roll Number" key and value of the Student dictionary
are deleted.
print(Student) : This print() function displays the Student variable.
Features of Dictionary Data Type
1. Key-Value Mapping : Yeh data ko anukram (index) ke bajay arthpurna naamon
(keys) se pehchanne ka tarika deti hai.Isse code ko padhna aur samajhna bohot aasan ho jata hai.
2. Ordered by Insertion (Python 3.7+) : Python 3.7 ke baad se dictionaries
elements ke jodne ke kram (order) ko yaad rakhti hain.Aap jis kram mein data dalenge, wo usi
kram mein hamesha print hogga.
3. Dynamic and Nestable : Dictionary ke andar aap kisi bhi tarah ka data (jaise
list ya dusri dictionary) store kar sakte hain.Ise Nested Dictionary kehte hain, jo complex data
handle karne ke kaam aati hai.