Computer >> Computer tutorials >  >> Programming >> Python

What is the difference between a python list and an array?


Basically, Python lists are very flexible and can hold completely heterogeneous, arbitrary data, and they can be appended to very efficiently, in amortized constant time. If you need to shrink and grow your array time-efficiently and without hassle, they are the way to go. But they use a lot more space than C arrays.

The array.array type, on the other hand, is just a thin wrapper on C arrays. It can hold only homogeneous data, all of the same type, and so it uses only sizeof(one object) * length bytes of memory.

So a list can be like: [1, 'a', [1, 2], 'string']

But an array can only contain things of the same type: [1, 2, 3, 4]