Memory Management in Lists and Tuples using Python
Last Updated :
20 Dec, 2024
In Python, lists and tuples are common data structures used to store sequences of elements. However, they differ significantly in terms of memory management, mutability, and performance characteristics.
This article will explore How Memory Management works in Lists and Tuples using Python.
Memory Allocation in Lists
Lists in Python are mutable sequences which means their elements can be modified after the list is created. Due to this mutability, lists in Python have some additional overhead.
- Dynamic Size: The memory for a list can grow or shrink as elements are added or removed.
- Overhead: Python lists are implemented as arrays of pointers to objects and thus they consume more memory compared to other simpler data structures like arrays.
- Internal Representation: Lists use a concept called over-allocation. When a list is created or extended, Python allocates more memory than is needed for the current elements. This minimizes the need for frequent resizing during list growth which improves performance but uses more memory.
Memory Management in Lists - Lists, unlike tuples, are mutable. To accommodate future additions, Python pre-allocates extra memory beyond the current size of the list.
- When the list is initialized, Python allocates additional empty slots in memory (e.g., 4 slots in this case). This allocation is beyond the size of the current elements, ensuring efficient resizing for subsequent additions.
- This extra allocation avoids the need to reallocate memory every time a new element is added. When the new element 'now' is added, it is inserted into one of the pre-allocated empty slots.
- The list now has 5 elements. This demonstrates the dynamic resizing capability of lists and how Python optimizes memory usage by reserving space for potential future growth.
Example:
Python
import sys
a = ['Learn', 'Python', 'with', 'GFG', 'now']
# Checking memory usage of the list
print("Memory size of list:", sys.getsizeof(a)) # Returns the size in bytes
OutputMemory size of list: 112
Memory Allocation in Tuples
Tuples, unlike lists are immutable, meaning their contents cannot be changed once they are created. This immutability provides certain optimizations in terms of memory management.
- Fixed Size: Once a tuple is created, its size is fixed which means no resizing is required.
- Lower Memory Overhead: Since tuples are immutable, Python can allocate memory more efficiently.
- Memory Efficiency: Tuples are more memory efficient when dealing with a large amount of data that doesn't require modification. The reduced overhead makes tuples faster in terms of memory access and iteration.
Memory Management in Tuple- Memory allocated for the tuple corresponds exactly to its size (4 elements). No extra memory is allocated for future growth because tuples are immutable.
- When a new element ('now') is added, a new tuple (Tnew) is created in memory. This tuple contains all the elements from the old tuple (T1) plus the new element. The original tuple (T1) remains unchanged.
- This process demonstrates that tuples require new memory allocation whenever their size changes.
Example:
Python
import sys
a = ('Learn', 'Python', 'with', 'GFG')
# Checking memory usage of the tuple
print("Memory size of tuple:", sys.getsizeof(a)) # Returns the size in bytes
OutputMemory size of tuple: 88
Comparison of Lists and Tuples in Terms of Memory Usage
When it comes to memory usage tuples are more efficient than lists due to their immutability. Lists being mutable require extra space to handle dynamic resizing and other internal operations.
Feature | List | Tuple |
---|
Mutability | Mutable(can be modified) | Immutable(cannot be modified) |
Memory Overhead | Higher(due to dynamic resizing) | Lower(fixed size) |
Performance | Slower(due to resizing & mutability) | Faster(due to immutability) |
Use case | Used when elements change frequently | Used when elements are fixed |
Similar Reads
Python | Find number of lists in a tuple Given a tuple of lists, the task is to find number of lists in a tuple. This is a very basic problem but can be useful while making some utility application. Method #1: Using len Python3 # Python code to find number of list in a tuple # Initial list Input1 = ([1, 2, 3, 4], [5, 6, 7, 8]) Input2 = ([1
4 min read
Python - Create list of tuples using for loop In this article, we will discuss how to create a List of Tuples using for loop in Python. Let's suppose we have a list and we want a create a list of tuples from that list where every element of the tuple will contain the list element and its corresponding index. Method 1: Using For loop with append
2 min read
Unzip List of Tuples in Python The task of unzipping a list of tuples in Python involves separating the elements of each tuple into individual lists, based on their positions. For example, given a list of tuples like [('a', 1), ('b', 4)], the goal is to generate two separate lists: ['a', 'b'] for the first elements and [1, 4] for
2 min read
Print a List of Tuples in Python The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ].Using print()print() function is the
2 min read
Python | Combining tuples in list of tuples Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways
7 min read