Some Guy
Some Guy

Reputation: 13568

How to know if you have a non-contiguous list in Python?

For arbitrarily large N, it isn't possible to store all the data in a list contiguously in memory.

For instance, in Python, if I do arr = [0] * N, for large enough N, this can't be contiguous.

What does Python do for this? I assume it gets stored non-contiguously. How does that work?

Upvotes: 0

Views: 294

Answers (1)

user2357112
user2357112

Reputation: 281585

CPython lists are always contiguous, at least in virtual memory. (There's little they could do to reasonably control physical contiguity and little reason to try.) CPython makes no attempt to break lists into noncontiguous segments in the face of memory fragmentation or anything like that.

If you want to see for yourself, take a look at Include/listobject.h and Objects/listobject.c. There's nothing in there about noncontiguous lists.

Upvotes: 1

Related Questions