As we know the array stores data in the memory in a contiguous manner i.e. the data(element of the array) stored is sequential and is not stored randomly at different addresses. And that's the reason why we cannot change the size of the array dynamically. But in the case of python lists, the size of the array can be changed when required. So, do python lists also store data in a contiguous way or do they use some different approach of storing data? Also, the size of all the data elements is also the same in an array for example in java or c++ i.e. all the elements of the array consume the same amount of memory and we clearly know that it isn't the case in python as we can store different data types in the same list. So, basically, my question is, what is the fundamental difference between lists in python and arrays in java(or any other language like c++ or c). I would really appreciate your help.
-
Also check out `java.util.ArrayList`. – Thomas Dec 10 '20 at 08:59
-
1note that in java, an array is an object. So you can't say python vs java et al because java is not similar to c and c++ in this regard – Samuel Dominguez Dec 10 '20 at 09:03
-
And note that in Java, an array can only hold primitives or references. Also fundamentally different from C and C++. Basically, the only things Java arrays and C arrays have in common is similar syntax and a fixed size. – Hulk Dec 10 '20 at 09:15
-
Python lists are... Lists. They have dynamic size, and thus are more similar to a Java [`ArrayList`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/ArrayList.html) or a C++ [`vector`](https://en.cppreference.com/w/cpp/container/vector). – Hulk Dec 10 '20 at 09:21
-
1Does this answer your question? [Does Python use linked lists for lists? Why is inserting slow?](https://stackoverflow.com/questions/12274060/does-python-use-linked-lists-for-lists-why-is-inserting-slow) – Hulk Dec 10 '20 at 09:21
-
Ohh, I got it now, thanks for the help to everyone – PaavanGupta Dec 10 '20 at 13:31
1 Answers
Although people compare Python lists to arrays in Java, actually lists are more like ArrayLists in Java (Or Vectors in C++). Lists in Python store pointers to objects rather than objects themselves, which is why they can store heterogenous types ([1,2,3,'x',"hello"]). So, technically the list still stores elements of specific size and type (pointers), but those pointers can point to objects of any type, size and value.
These variable length lists in Python keep a pointer to themselves and the their length in a list head structure, which work with exponential over-allocation, so that the code can have linear time complexity for append/remove operations.
Over-allocating the memory is used to avoid having to resize the list too many times (Say, after every append operation). The growth pattern of the list is something like: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, …
Similarly for remove/pop operations, if the new size is less than half the allocated size then the list is shrunk. Although additional cost of Slicing the list is added in case of removing the element from non-terminal positions.

- 21
- 4