[Tutor] array module
Alan Gauld
alan.gauld at yahoo.co.uk
Thu Oct 14 11:27:41 EDT 2021
On 14/10/2021 14:39, Leam Hall wrote:
> On 10/14/21 3:22 AM, Alan Gauld via Tutor wrote:
>
>> But personally, I've only used array once, mostly a regular
>> Python list does all that I need.
>
> Sorry for being dense, but I'm still moving between languages.
> How are a Python list and an array different?
A list is a built in data type, array is implemented as a module.
Arrays are homogenous, they only hold one type of data.
As the docs state:
"...compactly represent an array of basic values: characters,
integers, floating point numbers. Arrays are sequence types
and behave very much like lists, except that the type of
objects stored in them is constrained. The type is specified
at object creation time by using a type code, which is a
single character. "
They are therefore more resource efficient and allegedly faster to
access, although I've not seen much difference in that regard.
They have a few extra tricks compared to lists, such as the ability
to load values from strings or files.
On the downside they are limited to basic types and and the int
types are those supported by the C compiler not native Python
integers.
Useful if you have a lot of homogenous data to store/process.
I'm not sure what other use-cases exist.
For heavy processing or where top speed is required numpy
arrays are probably a better bet, but then you need to
install and import numpy...
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list