A dictionary is a collection in Python that stores data in key-value pairs. Dictionaries are useful when you want to store and retrieve values using meaningful keys.
A dictionary stores information in the form of key-value pairs.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(student)
Each dictionary item has two parts:
student = {
"name": "Rahul",
"age": 21
}
Here, name and age are keys, while Rahul and 21 are their values.
person = {
"name": "Priya",
"city": "Patna",
"age": 22
}
print(person)
An empty dictionary can be created using curly braces.
student = {}
print(student)
The type() function confirms that it is a dictionary.
student = {}
print(type(student))
You can access a value by using its key inside square brackets.
student = {
"name": "Amit",
"age": 20
}
print(student["name"])
print(student["age"])
The get() method can also be used to retrieve a value.
student = {
"name": "Rahul",
"age": 21
}
print(student.get("name"))
Using square brackets with a key that does not exist raises a KeyError.
student = {
"name": "Amit"
}
print(student["age"])
Using get() avoids this error and returns None by default.
print(student.get("age"))
You can add a new key-value pair by assigning a value to a new key.
student = {
"name": "Amit",
"age": 20
}
student["course"] = "Python"
print(student)
If a key already exists, assigning a new value updates that value.
student = {
"name": "Amit",
"age": 20
}
student["age"] = 21
print(student)
Dictionary keys must be unique. If the same key is written more than once, the later value replaces the earlier value.
student = {
"name": "Amit",
"name": "Rahul"
}
print(student)
Values in a dictionary do not have to be unique.
students = {
"student1": "Amit",
"student2": "Amit",
"student3": "Rahul"
}
print(students)
Use the len() function to find the number of key-value pairs.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(len(student))
Use the in operator to check whether a key exists.
student = {
"name": "Amit",
"age": 20
}
print("name" in student)
print("course" in student)
student = {
"name": "Amit",
"age": 20
}
print("course" not in student)
Dictionary values can contain different data types.
student = {
"name": "Amit",
"age": 20,
"marks": 85.5,
"passed": True
}
print(student)
A dictionary value can also be a list.
student = {
"name": "Amit",
"subjects": ["Python", "SQL", "HTML"]
}
print(student["subjects"])
A dictionary can contain another dictionary as a value. This is called a nested dictionary.
student = {
"name": "Amit",
"details": {
"age": 20,
"city": "Patna"
}
}
print(student["details"]["city"])
A for loop can be used to iterate through the keys of a dictionary.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
for key in student:
print(key)
Use the values() method to iterate through dictionary values.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
for value in student.values():
print(value)
Use the items() method to get both keys and values.
student = {
"name": "Amit",
"age": 20
}
for key, value in student.items():
print(key, ":", value)
The keys() method returns a view containing the dictionary's keys.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(student.keys())
The values() method returns a view containing the dictionary's values.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
print(student.values())
The items() method returns a view containing key-value pairs.
student = {
"name": "Amit",
"age": 20
}
print(student.items())
The del keyword can be used to remove a specific key-value pair.
student = {
"name": "Amit",
"age": 20,
"course": "Python"
}
del student["age"]
print(student)
Question: Which syntax is used to access a dictionary value using its key?