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

PythonServer Side ProgrammingProgramming

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]

raja
Published on 05-Jan-2018 17:09:28
Advertisements