Rupaalife 👈
Dictionary in Python
1. What is a Dictionary?
A Dictionary in Python is a built-in data type used to store data in the form of key–value
pairs.
Each key is used to access its corresponding value.
2. Why Do We Need a Dictionary?
Dictionary is used when:
Data has a meaningful label
We want fast access using a name instead of index
Data represents real-world objects with properties
Examples:
Student details
Employee records
Product information
3. How to Create a Dictionary
A dictionary is created using curly braces { } with key: value pairs.
Syntax:
dictionary_name = {key1: value1, key2: value2}
Example:
student = {
"name": "Ravi",
"age": 21,
"marks": 85
}
4. Real-Time Example (Easy Understanding)
Student Information
In real life, a student has details like:
Name
Roll number
Marks
Python representation:
student = {
"name": "Anita",
"roll_no": 101,
"marks": 92
}
Here:
Keys are labels
Values are actual data
5. Accessing Dictionary Values
Values are accessed using keys, not index numbers.
Example:
print(student["name"])
Output:
Anita
6. Modifying Dictionary Values
Dictionaries are mutable, meaning values can be changed.
Example:
student["marks"] = 95
print(student)
7. Adding New Key-Value Pair
Example:
student["grade"] = "A"
8. Removing Elements from Dictionary
Remove using pop()
[Link]("age")
Remove last item using popitem()
[Link]()
9. Dictionary Can Store Different Data Types
Example:
employee = {
"name": "Suresh",
"age": 30,
"salary": 45000.50,
"active": True
}
10. Important Dictionary Methods
Method Description
keys() Returns all keys
values() Returns all values
items()Returns key-value pairs
get() Safely access a value
update() Update dictionary
Example:
[Link]("marks")
11. Dictionary Keys Rules
Keys must be unique
Keys must be immutable (string, number, tuple)
Values can be any data type
12. Dictionary vs List vs Tuple
Feature List Tuple Dictionary
Data Storage ValuesValuesKey-Value
Index Based Yes Yes No
Mutable Yes No Yes
Access SpeedMedium Fast Very Fast
13. One-Line Definition (Exam Oriented)
A dictionary is a built-in data type in Python used to store data in key-value pairs.
14. When to Use Dictionary
Use dictionary when:
Data has labels
Fast lookup is required
Real-world structured data is needed
15. Conclusion
Dictionary is one of the most powerful data structures in Python.
It is widely used in real-time applications such as databases, APIs, and web development.