
Python
Learning Level
Different data structures have different speeds and memory usage. Choosing wisely matters!
Looking up a value is fastest with dictionaries and sets, slower with lists.
# Creating data structures
numbers_list = [1, 2, 3, 4, 5] # Takes O(n) to find an item
number_dict = {1: 'one', 2: 'two', 3: 'three'} # O(1) to find
number_set = {1, 2, 3, 4, 5} # O(1) to check membership
# Finding "3" in list requires checking each item
if 3 in numbers_list: # Slow for large lists
print("Found")
# Finding in dict/set is instant
if 3 in number_dict: # Fast, even for huge collections
print("Found")Use Lists for:
Use Dictionaries for:
Use Sets for:
Use Tuples for:
# Problem: Track which emails are registered
emails_list = ["alice@example.com", "bob@example.com"]
# ❌ Slow: "bob@example.com" in emails_list # Has to check every item
emails_set = {"alice@example.com", "bob@example.com"}
# ✅ Fast: "bob@example.com" in emails_set # Instant!
# Problem: Store book information by ISBN
books_list = [
{"isbn": "123", "title": "Python Basics"},
{"isbn": "456", "title": "Web Dev"}
]
# ❌ Slow: Need to loop to find by ISBN
books_dict = {
"123": {"title": "Python Basics", "author": "John"},
"456": {"title": "Web Dev", "author": "Jane"}
}
# ✅ Fast: books_dict["123"] gives instant access| Task | List | Dict | Set | Tuple |
|---|---|---|---|---|
| Access by position | Fast | — | — | Fast |
| Lookup by key | Slow | Very Fast | — | — |
| Check membership | Slow | Fast | Very Fast | Slow |
| Add item | Fast | Fast | Fast | N/A (immutable) |
| Remove item | Slow | Fast | Fast | N/A (immutable) |
| Memory | Low | High | High | Low |
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward