What is List and Tuple?
In Python, both list and tuple are used to store multiple values in a single variable. They are ordered collections, but the key difference is whether the data can be modified or not.
Key Differences
- Mutability β List can be changed, tuple cannot be changed after creation.
- Syntax β List uses [ ], tuple uses ( ).
- Performance β Tuple is faster because it is immutable.
- Memory β Tuple uses less memory than list.
- Use case β List for changing data, tuple for fixed data.
Example
# List (mutable) my_list = [10, 20, 30] my_list.append(40) my_list[0] = 99 # Tuple (immutable) my_tuple = (10, 20, 30) # my_tuple[0] = 99 β Error: cannot modify tuple
Memory and Performance
Tuples are stored in a fixed memory structure, which makes them more efficient. Lists require extra memory because they are designed to grow and shrink dynamically.
Tuples are not just βsmaller listsβ β they are optimized for fixed data storage.
Real-world example
- π List = Shopping cart (items can be added or removed)
- π Tuple = Aadhaar number / coordinates (fixed and cannot change)
When to use what?
Use List when:
- You need to modify data
- You need to add or remove items frequently
- Example: student names, shopping items
Use Tuple when:
- Data should not change
- You want better performance and safety
- Example: coordinates, fixed configuration values
Common mistake
Beginners often think tuple is just a βread-only list.β That is not fully correct. The real difference is that tuples are designed for immutability at the system level.
Summary
Lists are flexible and dynamic, while tuples are fixed and optimized. Use lists for changeable data and tuples for constant data to write efficient Python programs.