It is stated in the Python documentation that one of the advantages of namedtuple
is that it is as memory-efficient as tuples.
To validate this, I
A simpler metric is to check the size of equivalent tuple
and namedtuple
objects. Given two roughly analogous objects:
from collections import namedtuple
import sys
point = namedtuple('point', 'x y z')
point1 = point(1, 2, 3)
point2 = (1, 2, 3)
Get the size of them in memory:
>>> sys.getsizeof(point1)
72
>>> sys.getsizeof(point2)
72
They look the same to me...
Taking this a step further to replicate your results, notice that if you create a list of identical tuples the way you're doing it, each tuple
is the exact same object:
>>> test_list = [(1,2,3) for _ in range(10000000)]
>>> test_list[0] is test_list[-1]
True
So in your list of tuples, each index contains a reference the same object. There are not 10000000 tuples, there are 10000000 references to one tuple.
On the other hand, your list of namedtuple
objects actually does create 10000000 unique objects.
A better apples-to-apples comparison would be to view the memory usage for
>>> test_list = [(i, i+1, i+2) for i in range(10000000)]
and:
>>> test_list_n = [point(x=i, y=i+1, z=i+2) for i in range(10000000)]
They have the same size:
>>> sys.getsizeof(test_list)
81528056
>>> sys.getsizeof(test_list_n)
81528056