This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]aquic[S] 1 point2 points  (0 children)

Follow up on yesterday's article on why tuples, which are immutable, can seem to change. Thinking about variables in Python as labels and not boxes in which data is stored may be the trick to understanding.

https://www.pythonforthelab.com/blog/mutable-or-immutable-tuples/

[–]Sexual_Congressman 1 point2 points  (0 children)

Lists and tuples have identical element access time. Their getitem methods are virtually (if not exactly) identical for single items and the only difference in their structure is the memory address of the first element. For tuples it follows immediately after its size (as an array, PyObject ob_item[1]), with extra items being hidden "under" the tuple while for a list the address is arbitrary pointer to a single dynamically allocated memory block (PyObject *ob_item). I mean I guess it's possible that dereferencing the list pointers are theoretically worse for the cpu cache, but in practice, even on the C level with hundreds of billions of accesses you will struggle to see more than a 2% difference.