In Python, List and Tuple(Python document)
are both ordered collection data structures. While they may appear similar at first glance, they differ in terms of purpose and performance:
- List: Mutable and designed for dynamic operations where items need to be added, removed, or modified.
- Tuple: Immutable and ideal for fixed data collections, offering slightly better performance due to their immutability.
list
- Features: mutable, elements added/deleted
- Definition: using square brackets [].
Pros.
- Flexibility: Elements added, deleted, and modified.
- Versatile: Provides methods like append(), extend(), pop(), and more.
- Dynamic size: resized as needed.
Cons
- Memory usage: variable, so more memory required.
- Speed: Slower than tuple due to mutability.
tuple
- Features: immutable, unmodifiable
- Definition: use square brackets ()
Pros
- Immutability: Favorable for data protection, hashed (used as a dictionary key).
- Speed: Immutable, so memory and processing are efficient.
- Safety: Good for storing data that doesn’t need to change.
Disadvantages
- Inflexible: elements not added, deleted, or modified.
- Limited methods: fewer methods provided compared to list (e.g., no append()).
List and Tuple Code
colors = ['red', 'blue', 'black']
colors.append('white')
print(colors) # ['red', 'blue', 'black', 'white']
# insert list
colors.insert(1, 'yellow')
print(colors) # ['red', 'yellow', 'blue', 'black', 'white']
# edit list
colors[2] = 'gray'
print(colors) # ['red', 'yellow', 'gray', 'black', 'white']
# delete list
colors.remove('yellow')
print(colors) # ['red', 'gray', 'black', 'white']
# size of list
print(len(colors)) # 4
for color in colors:
print(color)
# slice
print(colors[1:3]) # ['gray', 'black']
print(type(colors))
colors = tuple(colors)
print(type(colors))
print(colors[0]) # 'red'
print(colors[1:3]) # ('gray', 'black')
# size of tuple
print(len(colors)) # 4
for color in colors:
print(color)
colors_list = list(colors)
print(colors_list) # ['red', 'gray', 'black', 'white']
Output
['red', 'blue', 'black', 'white']
['red', 'yellow', 'blue', 'black', 'white']
['red', 'yellow', 'gray', 'black', 'white']
['red', 'gray', 'black', 'white']
4
red
gray
black
white
['gray', 'black']
<class 'list'>
<class 'tuple'>
red
('gray', 'black')
4
red
gray
black
white
['red', 'gray', 'black', 'white']
Compare
Attribute | List | Tuple |
---|---|---|
Mutability | Mutable | Immutable |
Definition | Uses [] | Uses () |
Speed | Slower | Faster |
Memory Usage | Uses more memory | Uses less memory |
Use Case | For dynamic data | For fixed data |
Hashability | Not hashable | Hashable |
The speed difference between List and Tuple is usually negligible for small datasets, which is why it hasn’t caused issues for me so far. However, when working with larger datasets in the future, this difference might become more noticeable, depending on the operation.
While List and Tuple are technically “data structures,” I often use them more as collections of variables. This approach is common, which is why many people keep them around until the end of a function rather than deleting them immediately. However, I recommend deleting them as soon as they’re no longer needed, especially in memory-intensive scenarios, to free up resources and improve program efficiency.