DEV Community

Aditi Sharma
Aditi Sharma

Posted on

πŸš€ Day 6: Python Dictionaries & Comprehensions

Today, I focused on one of Python’s most versatile structures: Dictionaries.

πŸ”Ή What is a Dictionary?

Dictionaries store data as key-value pairs.

student = {"name": "Alice", "age": 22}
print(student["age"]) # 22

πŸ”Ή Dictionary Comprehensions

A Pythonic way to build dictionaries in one line.

squares = {x: x**2 for x in range(5)}
print(squares)

{0:0, 1:1, 2:4, 3:9, 4:16}

πŸ”Ή Merging Dictionaries

Python 3.5+ allows merging with unpacking.

d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}

merged = {**d1, **d2}
print(merged)

{'a':1, 'b':2, 'c':3, 'd':4}

🎯 Why Dictionaries Matter?
β€’ Fast lookups
β€’ Perfect for structured data
β€’ Comprehensions & merging make them concise and powerful

⚑ Tomorrow β†’ I’ll explore sets and set operations in Python.

Python #100DaysOfCode #DataStructures

Top comments (0)