
Show HN: Inside Python Dict – an explorable explanation - eatitraw
https://just-taking-a-ride.com/inside_python_dict/
======
stinos
_Python lists are actually arrays — contiguous chunks of memory._

Just wondering here, is this guaranteed to always be the case? Practically it
probably is, but does the Python spec (as in: the laguage, not one of it's
implementations) say a list must be implemented using contiguous memory of
slots with Python objects? That seems so low-level and C-ish. Or does the OP
actually mean _CPython_ here for instance?

~~~
johnisgood
According to this post:
[https://news.ycombinator.com/item?id=18707403](https://news.ycombinator.com/item?id=18707403),
"Lists aren't called arrays in Python because they're not arrays. Next!".

I really am confused now. Is it an array, or is it not?

~~~
joatmon-snoo
Strictly speaking, lists are not arrays.

I'm not aware of what the specification for a list would be, this is the
closest I'm aware of:
[https://docs.python.org/3/reference/datamodel.html#objects-v...](https://docs.python.org/3/reference/datamodel.html#objects-
values-and-types), 3.2 > Sequences > Mutable Sequences > Lists, "The items of
a list are arbitrary Python objects. Lists are formed by placing a comma-
separated list of expressions in square brackets"

CPython implementation is here:
[https://github.com/python/cpython/blob/e42b705188271da108de4...](https://github.com/python/cpython/blob/e42b705188271da108de42b55d9344642170aa2b/Include/listobject.h#L23-L40)

If I'm talking about arrays with someone else, I would expect us to be
thinking of arrays in the sense of C. Both C++ and Java use the same
definition. Other languages that I'm aware of (e.g. Javascript, Go) have their
own notions of arrays that you can't conflate with the C notion of an array.

From the C11 spec, §6.2.5.20: "An array type describes a contiguously
allocated nonempty set of objects with a particular member object type, called
the element type."

~~~
webmaven
Lists are very often used in the way arrays are, though.

The heterogeneous use-case (that in C, etc. is usually a struct) is often
filled by either tuples or (ordered) dictionaries in Python.

If you really want something a bit closer to a C array, you can fairly easily
create a UserList that enforces the non-empty requirement and only allows
items of a particular type.

~~~
dorfsmay
If you want some closer to C arrays, you can use Python array.array, which is
part of the standard library.

[https://docs.python.org/3/library/array.html](https://docs.python.org/3/library/array.html)

[https://stackoverflow.com/questions/176011/python-list-vs-
ar...](https://stackoverflow.com/questions/176011/python-list-vs-array-when-
to-use)

~~~
webmaven
Thanks! I had forgotten about that module.

------
whoisnnamdi
Thanks. Slowing realizing some of the benefits of dicts and trying to use them
more in my code where I would have otherwise defaulted to another data
structure.

They really are quite useful and intuitive to work with once you understand
basic structure

I do sometimes worry about speed relative to other options though, especially
in large chunks of data

------
deepakg
Related: [https://pyvideo.org/pycon-us-2017/modern-python-
dictionaries...](https://pyvideo.org/pycon-us-2017/modern-python-dictionaries-
a-confluence-of-a-dozen-great-ideas.html)

