Wednesday, 15 April 2026

April Python Bootcamp Day 10

 


Day 10: Dictionaries in Python – Mastering Key-Value Data

Dictionaries are one of the most powerful and widely used data structures in Python. They allow you to store and manage data in a key-value format, which makes them extremely efficient for searching, mapping, and organizing structured information.

If lists are about ordered collections, dictionaries are about meaningful relationships between data.


 What is a Dictionary?

A dictionary is an unordered, mutable collection of key-value pairs.

student = {
"name": "Piyush",
"age": 20,
"course": "Python"
}

Key Points:

  • Keys must be unique and immutable (string, number, tuple)
  • Values can be of any data type
  • Dictionaries are defined using {}

 Why Dictionaries are Important?

  • Fast lookup time: O(1) average complexity
  • Used in JSON data (APIs)
  • Core structure in backend and data science workflows
  • Ideal for representing real-world entities

 Creating Dictionaries

# Empty dictionary
data = {}

# Using dict()
data = dict(name="Piyush", age=20)

# Nested dictionary
student = {
"name": "Piyush",
"marks": {
"math": 90,
"science": 85
}
}

 Accessing Values

student = {"name": "Piyush", "age": 20}

print(student["name"]) # Direct access
print(student.get("age")) # Safe access

Difference:

  • [] → Raises error if key not found
  • .get() → Returns None (or default value)

 Accessing Nested Dictionary

student = {
"name": "Piyush",
"marks": {
"math": 90,
"science": 85
}
}

print(student["marks"]["math"]) # 90

Safe Way:

print(student.get("marks", {}).get("math"))

 Adding and Updating Values

student = {"name": "Piyush"}

# Add new key
student["age"] = 20

# Update existing key
student["name"] = "Rahul"

 Removing Elements

student = {"name": "Piyush", "age": 20}

student.pop("age")
del student["name"]
student.clear()

 Dictionary Methods

student = {"name": "Piyush", "age": 20}

student.keys()
student.values()
student.items()

 Looping Through Dictionary

student = {"name": "Piyush", "age": 20}

for key in student:
print(key, student[key])

for key, value in student.items():
print(key, value)

 Real-World Example: Frequency Counter

nums = [1, 2, 2, 3, 3, 3]

freq = {}
for num in nums:
freq[num] = freq.get(num, 0) + 1

print(freq)

Practice Questions

Basic

  1. Create a dictionary with 3 key-value pairs and print it
  2. Access a value using a key
  3. Add a new key-value pair
  4. Update an existing value

Intermediate

  1. Check if a key exists in a dictionary
  2. Print all keys and values separately
  3. Merge two dictionaries
  4. Count frequency of elements in a list using dictionary

Advanced

  1. Create a nested dictionary for 3 students with marks
  2. Sort a dictionary by keys and values
  3. Invert a dictionary (swap keys and values)
  4. Group elements based on frequency

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (243) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (29) Azure (10) BI (10) Books (262) Bootcamp (5) C (78) C# (12) C++ (83) Course (87) Coursera (300) Cybersecurity (30) data (5) Data Analysis (31) Data Analytics (22) data management (15) Data Science (343) Data Strucures (17) Deep Learning (150) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (19) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (69) Git (10) Google (51) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (283) Meta (24) MICHIGAN (5) microsoft (11) Nvidia (8) Pandas (14) PHP (20) Projects (32) pytho (1) Python (1299) Python Coding Challenge (1128) Python Mistakes (51) Python Quiz (472) Python Tips (5) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (49) Udemy (18) UX Research (1) web application (11) Web development (8) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)