Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
What's the difference between lists and tuples in Python?
Python has two main sequence data types for storing collections: lists and tuples. Both can store multiple items of different types, but they differ significantly in mutability, syntax, and use cases.
Key Differences Overview
| Feature | List | Tuple |
|---|---|---|
| Mutability | Mutable (changeable) | Immutable (unchangeable) |
| Syntax | Square brackets [ ] | Parentheses ( ) |
| Performance | Slower | Faster |
| Use Case | Dynamic data | Fixed data |
Creating Lists and Tuples
# Creating a list
fruits_list = ['apple', 'banana', 'orange']
print("List:", fruits_list)
# Creating a tuple
fruits_tuple = ('apple', 'banana', 'orange')
print("Tuple:", fruits_tuple)
# Single item tuple requires a comma
single_tuple = ('apple',)
print("Single tuple:", single_tuple)
List: ['apple', 'banana', 'orange']
Tuple: ('apple', 'banana', 'orange')
Single tuple: ('apple',)
Mutability Demonstration
Lists Are Mutable
# List operations - modifying elements
numbers_list = [1, 2, 3, 4]
print("Original list:", numbers_list)
# Modify an element
numbers_list[0] = 10
print("After modification:", numbers_list)
# Add an element
numbers_list.append(5)
print("After append:", numbers_list)
# Remove an element
numbers_list.remove(2)
print("After remove:", numbers_list)
Original list: [1, 2, 3, 4] After modification: [10, 2, 3, 4] After append: [10, 2, 3, 4, 5] After remove: [10, 3, 4, 5]
Tuples Are Immutable
# Tuple operations - attempting to modify
numbers_tuple = (1, 2, 3, 4)
print("Original tuple:", numbers_tuple)
# This will raise an error
try:
numbers_tuple[0] = 10
except TypeError as e:
print("Error:", e)
# This will also raise an error
try:
numbers_tuple.append(5)
except AttributeError as e:
print("Error:", e)
Original tuple: (1, 2, 3, 4) Error: 'tuple' object does not support item assignment Error: 'tuple' object has no attribute 'append'
Performance Comparison
import sys
# Memory usage comparison
sample_list = [1, 2, 3, 4, 5]
sample_tuple = (1, 2, 3, 4, 5)
print("List size in bytes:", sys.getsizeof(sample_list))
print("Tuple size in bytes:", sys.getsizeof(sample_tuple))
# Access time (tuples are slightly faster)
print("List type:", type(sample_list))
print("Tuple type:", type(sample_tuple))
List size in bytes: 104 Tuple size in bytes: 80 List type: <class 'list'> Tuple type: <class 'tuple'>
When to Use Each
Use Lists when:
- You need to modify the collection (add, remove, change items)
- The data represents a dynamic collection
- You need list methods like append(), remove(), sort()
Use Tuples when:
- The data should remain constant
- You need better performance and memory efficiency
- Using as dictionary keys (tuples are hashable)
- Returning multiple values from functions
Practical Examples
# Lists for dynamic data
shopping_cart = ['bread', 'milk']
shopping_cart.append('eggs') # Adding items
print("Shopping cart:", shopping_cart)
# Tuples for fixed data
coordinates = (10.5, 20.3) # GPS coordinates shouldn't change
rgb_color = (255, 128, 0) # Color values are fixed
print("Coordinates:", coordinates)
print("RGB Color:", rgb_color)
# Tuple as dictionary key
locations = {
(0, 0): "Origin",
(10, 20): "Point A"
}
print("Locations:", locations)
Shopping cart: ['bread', 'milk', 'eggs']
Coordinates: (10.5, 20.3)
RGB Color: (255, 128, 0)
Locations: {(0, 0): 'Origin', (10, 20): 'Point A'}
Conclusion
Lists are mutable and ideal for dynamic collections that need frequent modifications. Tuples are immutable, memory-efficient, and perfect for fixed data that shouldn't change. Choose based on whether your data needs to be modified after creation.
Advertisements
