Curriculum
Dictionaries are one of the most powerful and widely used data structures in Python. Dictionaries store data in key-value pairs, allowing fast access, efficient organization, and flexible data management. In Data Analytics, Data Science, Machine Learning, Business Analytics, and Software Development, Dictionaries are commonly used to represent structured data such as customer information, product details, employee records, and business metrics.
Unlike Lists and Tuples, which use indexes to access values, Dictionaries use unique keys.
Organizations use Dictionaries for:
Understanding Dictionaries is essential for working with structured data and real-world analytics projects.
A Dictionary is a collection of data stored as key-value pairs.
Example:
student = {
"name": "Rahul",
"age": 22,
"city": "Jaipur"
}
Here:
name, age, and city are keys."Rahul", 22, and "Jaipur" are values.Dictionaries are enclosed within curly braces {}.
Dictionaries provide:
Benefits:
Dictionaries are heavily used in Data Analytics.
Dictionaries have several important characteristics.
Data is stored as pairs.
Values can be modified.
New key-value pairs can be added.
Duplicate keys are not allowed.
Modern Python versions preserve insertion order.
These features make Dictionaries highly flexible.
Example:
employee = {
"name": "Priya",
"salary": 50000,
"department": "HR"
}
Example:
product = {
"id": 101,
"name": "Laptop",
"price": 55000
}
Dictionaries can store different data types.
Values are accessed using keys.
Example:
student = {
"name": "Rahul",
"age": 22
}
print(student["name"])
Output:
Rahul
Example:
print(student["age"])
Output:
22
Keys provide direct access to data.
The get() method safely retrieves values.
Example:
student = {
"name": "Rahul"
}
print(student.get("name"))
Output:
Rahul
Benefits:
Avoids errors when keys do not exist.
Dictionaries are mutable.
Example:
student = {
"name": "Rahul",
"age": 22
}
student["age"] = 23
print(student)
Output:
{'name': 'Rahul', 'age': 23}
Benefits:
Easy updates.
Example:
student = {
"name": "Rahul"
}
student["city"] = "Jaipur"
print(student)
Output:
{'name': 'Rahul', 'city': 'Jaipur'}
Applications:
Dynamic data storage.
Example:
student = {
"name": "Rahul",
"age": 22
}
student.pop("age")
print(student)
Output:
{'name': 'Rahul'}
Benefits:
Remove specific data.
Example:
student = {
"name": "Rahul",
"city": "Jaipur"
}
del student["city"]
print(student)
Output:
{'name': 'Rahul'}
Applications:
Data cleanup.
Example:
student = {
"name": "Rahul",
"age": 22
}
student.clear()
print(student)
Output:
{}
Benefits:
Resetting data.
Use the len() function.
Example:
student = {
"name": "Rahul",
"age": 22
}
print(len(student))
Output:
2
Applications:
Data validation.
Example:
student = {
"name": "Rahul",
"age": 22
}
print(student.keys())
Output:
dict_keys(['name', 'age'])
Applications:
Schema inspection.
Example:
student = {
"name": "Rahul",
"age": 22
}
print(student.values())
Output:
dict_values(['Rahul', 22])
Applications:
Data extraction.
Example:
student = {
"name": "Rahul",
"age": 22
}
print(student.items())
Output:
dict_items([('name', 'Rahul'), ('age', 22)])
Benefits:
Complete record access.
Example:
student = {
"name": "Rahul",
"age": 22
}
for key, value in student.items():
print(key, value)
Output:
name Rahul
age 22
Applications:
Data processing.
Dictionaries can contain other Dictionaries.
Example:
students = {
"student1": {
"name": "Rahul",
"age": 22
},
"student2": {
"name": "Priya",
"age": 21
}
}
print(students)
Applications:
Complex data storage.
Dictionary Comprehension creates Dictionaries efficiently.
Example:
squares = {
x: x * x
for x in range(5)
}
print(squares)
Output:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Benefits:
Shorter code.
Improved readability.
Data Analysts frequently use Dictionaries to store:
Example:
customer = {
"name": "Priya",
"revenue": 150000
}
Applications:
Business reporting.
Example:
customer = {
"name": "Rahul",
"total_purchase": 120000
}
print(customer["total_purchase"])
Output:
120000
Applications:
Customer segmentation.
Example:
financial_data = {
"revenue": 500000,
"expense": 300000
}
profit = (
financial_data["revenue"]
- financial_data["expense"]
)
print(profit)
Output:
200000
Applications:
Profitability analysis.
Machine Learning projects use Dictionaries for:
Benefits:
Efficient data organization.
Business Analysts use Dictionaries for:
Benefits:
Structured business information.
Characteristics:
Example:
student = {
"name": "Rahul"
}
Characteristics:
Example:
student = ["Rahul"]
Choose based on project requirements.
Example:
student["salary"]
Produces an error.
Example:
{
"name": "Rahul",
"name": "Amit"
}
The second value replaces the first.
May produce incorrect outputs.
Avoiding these mistakes improves code quality.
Improve readability.
Improve maintainability.
Reduce errors.
Improve collaboration.
Ensure reliability.
These practices support professional programming.
Benefits include:
Dictionaries are among the most powerful Python data structures.
After completing this lesson, you will be able to:
Dictionaries store data in key-value pairs.
Yes. Values can be modified.
Using keys.
A safe way to retrieve values from a Dictionary.
Yes. Nested Dictionaries are supported.
A concise way to create Dictionaries.
Because values are accessed using keys instead of searching through indexes.
They efficiently store and organize structured business and analytical data.
Want to master Python, SQL, Power BI, and Data Analytics?
WhatsApp us