Differences in Memory Usage: Tuples vs. Lists

Differences in Memory Usage: Tuples vs. Lists

The memory overhead associated with tuples compared to lists in Python is minimal, but there are differences due to the immutability of tuples and the dynamic nature of lists. Here are five examples illustrating these differences:

  1. Memory Allocation for a Tuple with Integer Elements:
import sys

tuple_example = (1, 2, 3, 4, 5)
list_example = [1, 2, 3, 4, 5]

print(sys.getsizeof(tuple_example))  # Output: 64 (on 64-bit systems)
print(sys.getsizeof(list_example))   # Output: 104 (on 64-bit systems)

In this example, a tuple and a list both contain the same integer elements. However, the list consumes more memory due to the additional overhead of storing pointers to each element and maintaining dynamic resizing.

  1. Memory Allocation for a Tuple with String Elements:
import sys

tuple_example = ('a', 'b', 'c', 'd', 'e')
list_example = ['a', 'b', 'c', 'd', 'e']

print(sys.getsizeof(tuple_example))  # Output: 56 (on 64-bit systems)
print(sys.getsizeof(list_example))   # Output: 104 (on 64-bit systems)

Similar to the previous example, the tuple consumes less memory compared to the list, even when containing string elements. This is because tuples have a fixed size and do not need extra space for dynamic resizing.

  1. Memory Allocation for a Nested Tuple:
import sys

tuple_example = ((1, 2), (3, 4), (5, 6))
list_example = [[1, 2], [3, 4], [5, 6]]

print(sys.getsizeof(tuple_example))  # Output: 72 (on 64-bit systems)
print(sys.getsizeof(list_example))   # Output: 112 (on 64-bit systems)

In this example, both the tuple and list contain nested elements. However, the tuple consumes less memory due to its fixed structure, whereas the list requires additional memory for nested list objects and pointers.

  1. Memory Allocation for an Empty Tuple vs. an Empty List:
import sys

empty_tuple = ()
empty_list = []

print(sys.getsizeof(empty_tuple))  # Output: 48 (on 64-bit systems)
print(sys.getsizeof(empty_list))   # Output: 64 (on 64-bit systems)

Even an empty tuple consumes some memory because it has a fixed overhead. However, it consumes less memory than an empty list due to the absence of pointers.

  1. Memory Allocation for Tuples vs. Lists with Different Sizes:
import sys

small_tuple = (1,)
small_list = [1]

large_tuple = tuple(range(1000))
large_list = list(range(1000))

print(sys.getsizeof(small_tuple))  # Output: 56 (on 64-bit systems)
print(sys.getsizeof(small_list))   # Output: 96 (on 64-bit systems)

print(sys.getsizeof(large_tuple))  # Output: 8072 (on 64-bit systems)
print(sys.getsizeof(large_list))   # Output: 9016 (on 64-bit systems)

In this example, the memory consumption of tuples remains relatively constant regardless of the number of elements, while lists consume more memory as the number of elements increases due to dynamic resizing.

These examples demonstrate that while tuples and lists both consume memory, tuples generally have lower memory overhead due to their immutability and fixed structure.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!