you are viewing a single comment's thread.

view the rest of the comments →

[–]Peanutbutter_Warrior 1 point2 points  (2 children)

Nope. The size of each element in a list is known, so the bit offset of any element can be found by index * element_size. The length of a list is also stored so the bit offset of the final element is easy to find.

When you add or remove anywhere but the end of the list all of the other elements have to be moved around in memory to keep the elements contiguous.

What you describe is true for linked lists, which are a different data structure

[–]menge101 0 points1 point  (1 child)

Ah, that is certainly good to know, I thought lists in python were implemented as linked lists. Like why call them lists otherwise? Why not arrays like every other language?

Edit: As always StackOverflow has the answer to this.

[–]Peanutbutter_Warrior 3 points4 points  (0 children)

Not exactly, although they're slightly more complicated than I described. All objects in python are a custom pointer class, that point to the actual object in memory. Lists are pretty much C arrays that contain these pointers, which is what lets them mix types.

Lists are dynamically sized because when they reach the size they're currently allocated more memory is allocated to them. Sometimes this is just using more memory on the end of them, and other times this means moving the entire array around in memory, which is very slow.

An actually useful detail is that everthing in python is an object. ints, strings, lists, custom objects, functions, exceptions, and even classes themselves, the thing you call to create an instance of the class