[[+content_image]]
D
D
DVoropaev2019-03-21 09:01:24
Python
DVoropaev, 2019-03-21 09:01:24

Why, when two ways of specifying the same list, does it take up a different amount of memory?

>>> a = [[i for i in range(0, 5)] for j in range(0, 2)]
>>> a
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> sys.getsizeof(a)
96

>>> a = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> a
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
>>> sys.getsizeof(a)
80

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2019-03-21
@LazyTalent

list() uses more memory than list comprehension

A
aderes, 2019-03-21
@de-iure

because they are different objects... in the first case it is a list generator, in the second it is a list
>>> a = [[i for i in range(0, 5)] for j in range(0, 2)]
>>> a
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] >>>
sys.getsizeof(a ) memory that is directly occupied by the generator itself ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question