A dictionary provides several built in methods for managing and manipulating key-value pairs efficiently.
1. len()
The len() function returns the total number of key-value pairs in a dictionary.
Syntax
len(dictionary)
Example
student = {
"name": "Kaif",
"age": 20,
"course": "Python"
}
print(len(student))
Output
3
Uses
- Counting dictionary items.
- Controlling loops.
- Checking dictionary size.
2. str()
The str() function converts a dictionary into its string representation.
Syntax
str(dictionary)
Example
student = {
"name": "Kaif",
"age": 20
}
print(str(student))
Output
"{'name': 'Kaif', 'age': 20}"
Uses
- Converting dictionary to string.
- Displaying dictionary data.
- Logging and debugging.
3. clear()
The clear() method removes all items from a dictionary.
Syntax
dictionary.clear()
Example
student = {
"name": "Kaif",
"age": 20
}
student.clear()
print(student)
Output
{}
Characteristics
- Deletes all key-value pairs.
- Dictionary remains but becomes empty.
4. copy()
The copy() method creates a shallow copy of a dictionary.
Syntax
dictionary.copy()
Example
student = {
"name": "Kaif",
"age": 20
}
new_student = student.copy()
print(new_student)
Output
{'name': 'Kaif', 'age': 20}
Characteristics
- Creates a separate dictionary.
- Changes in one dictionary do not affect the other.
5. get()
The get() method returns the value associated with a specified key.
Syntax
dictionary.get(key)
Example
student = {
"name": "Kaif",
"age": 20
}
print(student.get("name"))
Output
Kaif
If Key Does Not Exist
print(student.get("city"))
Output
None
Advantages
- Prevents KeyError.
- Safer than direct key access.
- Returns
Noneif key is absent.
6. update()
The update() method adds new items or modifies existing items.
Syntax
dictionary.update(other_dictionary)
Example 1: Adding Items
student = {
"name": "Kaif",
"age": 20
}
student.update({"city": "Mumbai"})
print(student)
Output
{'name': 'Kaif', 'age': 20, 'city': 'Mumbai'}
Example 2: Modifying Items
student = {
"name": "Kaif",
"age": 20
}
student.update({"age": 21})
print(student)
Output
{'name': 'Kaif', 'age': 21}
Characteristics
- Adds new key-value pairs.
- Updates existing values.
- Modifies the original dictionary.
Summary Table
| Method | Purpose |
|---|---|
| len() | Returns number of key-value pairs |
| str() | Converts dictionary to string |
| clear() | Removes all items |
| copy() | Creates a copy of dictionary |
| get() | Retrieves value using key |
| update() | Adds or modifies items |
Difference Between List and Dictionary:
| Basis | List | Dictionary |
|---|---|---|
| Definition | Collection of elements | Collection of key-value pairs |
| Symbol | [ ] | { } |
| Access Method | Index | Key |
| Ordering | Ordered | Ordered (Python 3.7+) |
| Modification | Mutable | Mutable |
| Duplicate Values | Allowed | Values allowed, keys unique |
| Searching | By index | By key |
| Storage Format | Single values | Key-value pairs |
| Example | [10, 20, 30] | {“a”:10, “b”:20} |
| Retrieval Speed | Slower for searching | Faster using keys |
Example of List
fruits = ["Apple", "Mango", "Orange"]
print(fruits[1])
Output
Mango
Example of Dictionary
student = {
"name": "Kaif",
"age": 20
}
print(student["name"])
Output
Kaif