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)