2

I created two separate lists a and b with same element in two different ways. Why is there a difference between the size of the two lists?

import sys
a = [0]
print(a)
>>> [0]
print(sys.getsizeof(a))
>>> 72

b = [i for i in range(1)]
print(b)
>>> [0]
print(sys.getsizeof(b))
>>> 96
Sheldore
  • 37,862
  • 7
  • 57
  • 71
Sunny
  • 39
  • 3
  • 3
    You're experiencing over-allocation. [Related](https://stackoverflow.com/questions/7247298/size-of-list-in-memory) – user3483203 Jun 25 '19 at 18:45
  • Also related: https://stackoverflow.com/questions/51526242/why-do-two-identical-lists-have-a-different-memory-footprint/51526419 I would even consider that one a duplicate. But TL:DR the list literal allocates the udnerlying buffer to exactly the length required to hold that list, whereas a list-comphrenesion essentially does a for-loop with an `.append`, which will use overallocation. – juanpa.arrivillaga Jun 25 '19 at 18:49
  • thanks @juanpa.arrivillaga marked as so! – Devesh Kumar Singh Jun 25 '19 at 18:52
  • Another related question that really gets into the internals: https://stackoverflow.com/questions/40018398/list-uses-more-memory-than-list-comprehension – juanpa.arrivillaga Jun 25 '19 at 18:58

1 Answers1

2

When the interpreter sees a = [0], it knows that it can build a list with just one element.

When it does a list comprehension, it first creates an empty list, then appends items as it goes. It doesn't know beforehand how big the list is going to be, even if it's iterating over something straightforward like range(1). So it tries to guess how much memory to allocate, and if it turns out that's not enough, it will have to dynamically increase the memory allocation. That's not cheap, so it may start with a generous guess.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89