![]() |
How To Use Python Dictionary Explain Briefly Details With Examples |
How To Use Python Dictionary Explain Briefly Details With Examples
Python Dictionary: A Dictionary in Python as you guys all know Python programming language is one of the most popular programming languages.
nowadays it provides us with numerous applications and various out-of-the-box features one such concept is the dictionary in Python.We use a dictionary then we will compare the dictionary with the lists moving further.
Important Variable Topic in Other Languages
👉C++ Variables
👉Java Variables
👉C Variables
Dictionary: Python so what is the dictionary in Python it is a collection datatype just like a set or a list but certain features make a dictionary unique so let's take a look at the features of the Python dictionary, first of all, it is unordered.
We can change values in a dictionary as well since it is mutable. also, it has key-value pairs which are like a map.
Why do we use a dictionary in Python?
1. First of all the reason would be it is unordered and stores data like a map.where we have distinct values and corresponding to these values there are respective definitions in the case of the dictionary even though.
How we implement a dictionary in Python
we will try to implement a dictionary that takes the name of the dictionary as a so inside the curly brackets.when I am specifying the key-value pairs I have to use the colon to separate the key from the value now I'll give one value.
Syntax of Dictionary
dict_name={
key1:value1,key2:value2,key3:value3,.....
}
dict_name={
key1:value1,key2:value2,key3:value3,.....
}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print(student)
*****OUTPUT*****
{'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print(student)
*****OUTPUT*****
{'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
Access Element of Dictionary: Python dictionary can be accessed elements of dictionary using the key name.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student["name"])
print("Student Roll No:",student["rollno"])
print("Student Email:",student["email"])
print("Student address:",student["address"])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student["name"])
print("Student Roll No:",student["rollno"])
print("Student Email:",student["email"])
print("Student address:",student["address"])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
Access get Method in Dictionary: Python dictionary can be used get Method applied key name. it is very useful Method in Python dictionary.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student.get("name"))
print("Student Roll No:",student.get("rollno"))
print("Student Email:",student.get("email"))
print("Student address:",student.get("address"))
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Student Name:",student.get("name"))
print("Student Roll No:",student.get("rollno"))
print("Student Email:",student.get("email"))
print("Student address:",student.get("address"))
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student address: Delhi
Access Value Loop: Python gets the value using Loop.it can be used key and value pair in this method.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
for value in student:
print("Student Name:",vlaue,student[value])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student Address: Delhi
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
for value in student:
print("Student Name:",vlaue,student[value])
*****OUTPUT*****
Student Name: icoderweb
Student Roll No: 807714
Student Email: icoderweb@gmail.com
Student Address: Delhi
Update Dictionary Value Python dictionary changeable. Dictionary is mutable it can be easily updated using a key name.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Update Dictionary:",student)
student["name"]="Python"
print("After Update Dictionary:",student)
*****OUTPUT*****
Before Update Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Update Dictionary: {'name': 'Python', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Update Dictionary:",student)
student["name"]="Python"
print("After Update Dictionary:",student)
*****OUTPUT*****
Before Update Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Update Dictionary: {'name': 'Python', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
Length of dictionary: Python len function can be used to get the length of any dictionary. Length function gets the information of present value in the dictionary.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Lenght of Dictionary:",len(student))
*****OUTPUT*****
Lenght of Dictionary: 4
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Lenght of Dictionary:",len(student))
*****OUTPUT*****
Lenght of Dictionary: 4
Add Elements in DictionaryPython add can be used to add new elements in a dictionary using the new key.
How To Add Elements Dictionary in Python?
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Add Element in Dictionary:",student)
student["dob"]='02/03/2000'
print("After Add Element in Dictionary:",student)
*****OUTPUT*****
Before Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi', 'dob': '02/03/2000'}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Add Element in Dictionary:",student)
student["dob"]='02/03/2000'
print("After Add Element in Dictionary:",student)
*****OUTPUT*****
Before Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi', 'dob': '02/03/2000'}
Delete Element from pop() Function dictionary
The python pop() function can be used to remove specified elements from the dictionary.pop Method used any key elements remove from the dictionary.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Delete Element in Dictionary:",student)
student("email")
print("After Delete Element in Dictionary:",student)
*****OUTPUT*****
Before Delete Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'address': 'Delhi'}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Delete Element in Dictionary:",student)
student("email")
print("After Delete Element in Dictionary:",student)
*****OUTPUT*****
Before Delete Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'address': 'Delhi'}
Delete Element using del keyboard dictionary
Pythonkeywordsd can be used to remove specified elements from the dictionary. The method used any key elements remove from the dictionary.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Delete Element in Dictionary:",student)
del student["email"]
print("After Delete Element in Dictionary:",student)
*****OUTPUT*****
Before Delete Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'address': 'Delhi'}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Delete Element in Dictionary:",student)
del student["email"]
print("After Delete Element in Dictionary:",student)
*****OUTPUT*****
Before Delete Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Add Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'address': 'Delhi'}
Clear Dictionary: Python clear dictionary can be used to empty the dictionary.
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Clear Element in Dictionary:",student)
student.clear()
print("After Clear Element in Dictionary:",student)
*****OUTPUT*****
Before Clear Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Clear Element in Dictionary: {}
student={
"name":"icoderweb",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
}
print("Before Clear Element in Dictionary:",student)
student.clear()
print("After Clear Element in Dictionary:",student)
*****OUTPUT*****
Before Clear Element in Dictionary: {'name': 'icoderweb', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
After Clear Element in Dictionary: {}
Nested Dictionary: Python nested dictionary can be used to another dictionary list data in Nested dictionary.
How To use Nested Dictionary in Python?
student={
"Ravi":{
"name":"Ravi",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
},
"Baby":{
"name":"Baby",
"city":"Rampur",
"address":"Kolkata"
}
}
print("Ravi Information:",student["Ravi"])
print("Baby Information:",student["Baby"])
*****OUTPUT*****
Ravi Information: {'name': 'Ravi', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
Baby Information: {'name': 'Baby', 'city': 'Rampur', 'address': 'Kolkata'}
student={
"Ravi":{
"name":"Ravi",
"rollno":807714,
"email":"icoderweb@gmail.com",
"address":"Delhi"
},
"Baby":{
"name":"Baby",
"city":"Rampur",
"address":"Kolkata"
}
}
print("Ravi Information:",student["Ravi"])
print("Baby Information:",student["Baby"])
*****OUTPUT*****
Ravi Information: {'name': 'Ravi', 'rollno': 807714, 'email': 'icoderweb@gmail.com', 'address': 'Delhi'}
Baby Information: {'name': 'Baby', 'city': 'Rampur', 'address': 'Kolkata'}