"It depends." Python allocates space for lists in such a way as to achieve amortized constant time for appending elements to the list.
In practice, what this means with the current implementation is... the list always has space allocated for a power-of-two number of elements. So range[1000000] will actually allocate a list big enough to hold 2^20 elements [~ 1.045 million].
This is only the space required to store the list structure itself [which is an array of pointers to the Python objects for each element]. A 32-bit system will require 4 bytes per element, a 64-bit system will use 8 bytes per element.
Furthermore, you need space to store the actual elements. This varies widely. For small integers [-5 to 256 currently], no additional space is needed, but for larger numbers Python allocates a new object for each integer, which takes 10-100 bytes and tends to fragment memory.
Bottom line: it's complicated and Python lists are not a good way to store large homogeneous data structures. For that, use the array
module or, if you need to do vectorized math, use NumPy.
PS- Tuples, unlike lists, are not designed to have elements progressively appended to them. I don't know how the allocator works, but don't even think about using it for large data structures :-]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In Python, a list is a collection data type that can store elements in an ordered manner and can also have duplicate elements. The size of a list means the amount of memory [in bytes] occupied by a list object. In this article, we will learn various ways to get the size of a python list.
1.Using getsizeof[]
function:
The getsizeof[]
function belongs to the python’s sys module. It has been implemented in the below example.
Example 1:
import
sys
list1
=
[
1
,
2
,
3
,
5
]
list2
=
[
"GeeksForGeeks"
,
"Data Structure"
,
"Algorithms"
]
list3
=
[
1
,
"Geeks"
,
2
,
"For"
,
3
,
"Geeks"
]
print
[
"Size of list1: "
+
str
[sys.getsizeof[list1]]
+
"bytes"
]
print
[
"Size of list2: "
+
str
[sys.getsizeof[list2]]
+
"bytes"
]
print
[
"Size of list3: "
+
str
[sys.getsizeof[list3]]
+
"bytes"
]
Output:
Size of list1: 96bytes Size of list1: 88bytes Size of list1: 112bytes
Note:The sys.getsizeof[]
function includes the marginal space usage, which includes the garbage collection overhead for the object. Meaning it returns the total space occupied by the object in addition to the garbage collection overhead for the spaces being used.
1.Using inbuilt __sizeof__[]
method:
Python also has an inbuilt __sizeof__[] method to
determine the space allocation of an object without any additional garbage value. It has been implemented in the below example.
Example 2:
list1
=
[
1
,
2
,
3
,
5
]
list2
=
[
"GeeksForGeeks"
,
"Data Structure"
,
"Algorithms"
]
list3
=
[
1
,
"Geeks"
,
2
,
"For"
,
3
,
"Geeks"
]
print
[
"Size of list1: "
+
str
[list1.__sizeof__[]]
+
"bytes"
]
print
[
"Size of list2: "
+
str
[list2.__sizeof__[]]
+
"bytes"
]
print
[
"Size of list3: "
+
str
[list3.__sizeof__[]]
+
"bytes"
]
Output:
Size of list1: 72bytes Size of list1: 64bytes Size of list1: 88bytes