Dictionary is a collection of data stored in key-value pairs. Dictionaries are enclosed within curly braces { } and are mutable, meaning their contents can be changed after creation.
Features of Dictionary:
- Stores data as key-value pairs.
- Mutable in nature.
- Keys must be unique.
- Values can be duplicated.
- Elements are accessed using keys.
- Dictionaries are dynamic and can grow or shrink.
Example
student = {
"name": "Kaif",
"age": 20,
"course": "Python"
}
Creating a Dictionary:
Method 1: Using Curly Braces
student = {
"name": "Kaif",
"age": 20,
"course": "Python"
}
print(student)
Output
{'name': 'Kaif', 'age': 20, 'course': 'Python'}
Method 2: Using dict() Function
student = dict(name="Kaif", age=20, course="Python")
print(student)
Output
{'name': 'Kaif', 'age': 20, 'course': 'Python'}
student = {}
print(student)
Output
{}
Accessing Values in a Dictionary:
Dictionary values are accessed using their keys.
Using Square Brackets
Example
student = {
"name": "Kaif",
"age": 20,
"course": "Python"
}
print(student["name"])
print(student["age"])
Output
Kaif
20
Using get() Method
The get() method returns the value of the specified key.
Example
student = {
"name": "Kaif",
"age": 20
}
print(student.get("name"))
Output
Kaif
Advantage
If the key does not exist, get() returns None instead of generating an error.
print(student.get("city"))
Output
None
Adding Items to a Dictionary:
New key-value pairs can be added by assigning a value to a new key.
Syntax
dictionary[key] = value
Example
student = {
"name": "Kaif",
"age": 20
}
student["city"] = "Mumbai"
print(student)
Output
{'name': 'Kaif', 'age': 20, 'city': 'Mumbai'}
Modifying Items in a Dictionary:
Existing values can be changed by assigning a new value to an existing key.
Example
student = {
"name": "Kaif",
"age": 20
}
student["age"] = 21
print(student)
Output
{'name': 'Kaif', 'age': 21}
Characteristics
- Only the value changes.
- Key remains the same.
- Dictionary is modified directly.
Deleting Items from a Dictionary:
Items can be removed using different methods.
1. del Statement
Removes a specific key-value pair.
Example
student = {
"name": "Kaif",
"age": 20,
"city": "Mumbai"
}
del student["city"]
print(student)
Output
{'name': 'Kaif', 'age': 20}
2. pop() Method
Removes and returns the value of a specified key.
Example
student = {
"name": "Kaif",
"age": 20
}
age = student.pop("age")
print(age)
print(student)
Output
20
{'name': 'Kaif'}
3. popitem() Method
Removes and returns the last inserted key-value pair.
Example
student = {
"name": "Kaif",
"age": 20,
"city": "Mumbai"
}
student.popitem()
print(student)
Output
{'name': 'Kaif', 'age': 20}
4. clear() Method
Removes all items from the dictionary.
Example
student = {
"name": "Kaif",
"age": 20
}
student.clear()
print(student)
Output
{}
| Operation | Syntax/Method | Purpose |
|---|---|---|
| Create Dictionary | {} or dict() |
Creates a dictionary |
| Access Value | dict[key] |
Retrieves value |
| Safe Access | get() |
Retrieves value safely |
| Add Item | dict[key] = value |
Adds new key-value pair |
| Modify Item | dict[key] = new_value |
Changes existing value |
| Delete Item | del dict[key] |
Removes item |
| Remove Item | pop() |
Removes and returns value |
| Remove Last Item | popitem() |
Removes last inserted pair |
| Delete All Items | clear() |
Empties dictionary |
Advantages of Dictionary:
- Fast data retrieval using keys.
- Easy to add, update, and delete items.
- Stores data in key-value format.
- Suitable for structured data storage.
- Supports nested dictionaries.