
Python
Learning Level
Now that you know each structure, understand when to use each one based on your needs.
| Feature | List | Dictionary | Set | Tuple |
|---|---|---|---|---|
| Ordered? | ✅ Yes | ✅ Yes (3.7+) | ❌ No | ✅ Yes |
| Mutable? | ✅ Yes | ✅ Yes | ✅ Yes | ❌ No |
| Hashable? | ❌ No | ❌ No | ❌ No | ✅ Yes |
| Lookup Speed | Slow O(n) | Fast O(1) | Fast O(1) | Slow O(n) |
| Duplicates? | ✅ Allowed | Keys unique | ❌ Auto-removed | ✅ Allowed |
| Dict Key? | ❌ No | ❌ No | ❌ No | ✅ Yes |
| Memory | Low | High | High | Very Low |
Use Lists When:
# Student roster in order
roster = ["Alice", "Bob", "Carol"]
roster.append("David")
print(roster[0]) # "Alice"Use Dictionaries When:
# Store person information by ID
people = {
"alice": {"age": 30, "city": "NYC"},
"bob": {"age": 25, "city": "LA"}
}
print(people["alice"]["age"]) # 30Use Sets When:
# Unique visitor tracking
visitors = {"alice", "bob", "alice", "carol"}
print(len(visitors)) # 3 (auto-deduplicated)
if "alice" in visitors: # Fast check
print("Welcome back!")Use Tuples When:
# Fixed coordinates
coordinates = (40.7128, -74.0060)
locations = {coordinates: "New York"}
def get_user():
return ("Alice", 30, "NYC") # Multiple return valuesSTART: Choose a data structure
1. Does order matter?
YES → Use List or Tuple
NO → Use Set
2. Will data change?
YES → Use List
NO → Use Tuple
3. Need to look up by key?
YES → Use Dictionary
NO → Continue
4. Check membership often?
YES → Use Set (faster)
NO → Use List (less memory)| Task | Best | Second | Avoid |
|---|---|---|---|
| Access by position | List O(1) | Tuple O(1) | Dict, Set |
| Lookup by key | Dict O(1) | Set O(1) | List O(n) |
| Check membership | Set O(1) | Dict O(1) | List O(n) |
| Add/remove item | List O(1) | Set O(1) | Tuple (impossible) |
| Store fixed data | Tuple | List | Dict, Set |
| Use as dict key | Tuple | None other | List, Dict, Set |
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