programming#python#data-structures
Python Data Structures: Lists, Dicts and Sets
The three collections you'll use every single day — with real examples.
Series
Python from Scratch
Python Data Structures
List — an ordered bag
cities = ["Delhi", "Mumbai", "Kolkata"]
cities.append("Chennai")
print(cities[0]) # Delhi
print(cities[-1]) # Chennai (negative index = from the end)
print(len(cities)) # 4
Dict — a lookup table
student = {"name": "Priya", "roll": 42, "cgpa": 8.7}
print(student["name"]) # Priya
student["email"] = "priya@college.edu"
# safer lookup that doesn't crash on missing keys
print(student.get("phone", "N/A")) # N/A
Set — a bag of unique things
skills = {"python", "sql", "python"} # duplicate removed
skills.add("docker")
print("python" in skills) # True (fast!)
When to use which
| Need | Use |
|---|---|
| Ordered items, allow duplicates | list |
| Look up a value by key | dict |
| Deduplicate + membership check | set |
Real-world example
Reading a CSV of college students → a list of dicts is the natural shape:
students = [
{"name": "Priya", "cgpa": 8.7},
{"name": "Rahul", "cgpa": 7.9},
]
top = sorted(students, key=lambda s: s["cgpa"], reverse=True)[0]
print(top["name"]) # Priya
Keep reading
You may also like
programming
YAML Explained with Examples
YAML is everywhere in DevOps (Docker Compose, Kubernetes, GitHub Actions). Learn its 6 rules and you're done.
Read
programming
Python for Absolute Beginners
The absolute essentials of Python — variables, if, loops and functions — in the clearest possible words, with runnable examples.
Read
programming
Python Async: A Practical Guide
asyncio, event loops and when NOT to use async.
Read
Discussion (0)
No comments yet. Be the first to weigh in.