1. Lists: The Mutable Workhorse
Practical Example: Shopping Cart
# Create shopping cart
shopping_cart = ["apples", "milk", "bread"]
# Modify cart
shopping_cart.append("eggs")
shopping_cart.remove("milk")
shopping_cart[1] = "whole wheat bread"
When to Use: Dynamic data, frequent modifications, ordered collections
2. Tuples: The Immutable Container
Practical Example: GPS Coordinates
# Store location coordinates
office_location = (40.7128, -74.0060)
# Access values
latitude = office_location[0]
longitude = office_location[1]
When to Use: Fixed data, dictionary keys, memory efficiency
3. Dictionaries: Key-Value Storage
Practical Example: User Profile
user_profile = {
"username": "python_dev",
"skills": ["Python", "SQL", "AWS"],
"experience": 5,
"active": True
}
# Access data
print(user_profile["skills"]) # Output: ['Python', 'SQL', 'AWS']
When to Use: Key-based access, complex objects, fast lookups
Comparison Table
Feature | List | Tuple | Dictionary |
---|---|---|---|
Mutability | Mutable | Immutable | Mutable |
Order | Ordered | Ordered | Unordered |
Syntax | [] | () | {} |
Best Use Case | Dynamic collections | Fixed data | Key-value pairs |
Real-World Scenario: E-commerce System
# Product tuple (immutable)
PRODUCT = ("Laptop", 999.99, "electronics")
# Cart list (mutable)
cart = [{
"id": 101,
"name": PRODUCT[0],
"price": PRODUCT[1]
}]
# User dictionary
user = {
"name": "John Doe",
"cart": cart,
"discount": 0.15
}
# Calculate total
total = sum(item["price"] for item in user["cart"])
final_price = total * (1 - user["discount"])
When to Choose Which?
- List: Changing data, multiple same-type items
- Tuple: Unchanging data, safer data storage
- Dictionary: Labeled data, complex relationships