Lists and tuples are both sequence data types in Python that can store multiple items.
However, the key difference lies in their mutability and usage:
| Feature | List | Tuple |
|---|---|---|
| Mutability | Mutable (can be changed after creation) | Immutable (cannot be changed once created) |
| Syntax | Defined using [] | Defined using () |
| Performance | Slower (due to mutability) | Faster (due to immutability) |
| Use Case | Suitable for data that changes | Suitable for fixed data |
| Methods | Many (e.g., append(), remove(), sort()) | Few (e.g., count(), index()) |
🧩 Example:
# List Example
my_list = [1, 2, 3]
my_list.append(4) # Modifying the list
print("List:", my_list)
# Tuple Example
my_tuple = (1, 2, 3)
# my_tuple.append(4) # ❌ Error: Tuples are immutable
print("Tuple:", my_tuple)Output:
List: [1, 2, 3, 4]
Tuple: (1, 2, 3)