3 ms·
Python lists aren’t arrays, right? They are the closest thing in Python to an array maybe, but they do a ton of stuff under the hood, like grow when needed. Ca
by bee_rider 2y ago
Python lists aren’t arrays, right? They are the closest thing in Python to an array maybe, but they do a ton of stuff under the hood, like grow when needed.
Calling them arrays would be very confusing to everybody who expects a typical array: a pointer with some empty space after it.
- Phrodo_00 2y agoYeah and no? Python Lists indeed don't make any sort of array-like guarantee, but they're implemented as a vector/autogrowing-array of python object references (but these objects are not guaranteed to be cache-local). The implementation defines the underlying data structure as PyObject *ob_item
- Sn3llius 2y agoList access is O(1), which effectively makes them arrays :)
- Phrodo_00 2y agoMaybe if you don't consider CPU architecture, but most would expect to be able to do loops over Arrays that don't incur in a lot of cache misses, and Python Lists don't do that, since they're actually arrays of pointers to heap memory.
- Sn3llius 2y agoMost languages have arrays that grow automatically. I'd say C/C++ is the exception there. When I said array, I specifically meant O(1) access, which is in contrast to linked lists, which the name "list" would seem to imply.
- pjmlp 2y agoC++ has them on the standard library, I would make a clear split with C in this regard.