[Tutor] dynamic arrays?

Dave Angel davea at ieee.org
Mon Sep 27 18:12:10 CEST 2010



On 2:59 PM, Alex Hall wrote:
> On 9/27/10, Brian Jones<bkjones at gmail.com>  wrote:
>> On Mon, Sep 27, 2010 at 11:32 AM, Alex Hall<mehgcap at gmail.com>  wrote:
>>
>>> Hi all,
>>> One thing I have never much liked about Python is its need for
>>> specifically sized arrays and lack of a dynamic, array-like data
>>> structure. For example, the following fails with a "list assignment
>>> index out of range" error:
>>>
>>> a=[]
>>> i=0
>>> for l in open("file.txt", "r"):
>>>   a[i]=l
>>>    i+=1
>>>
>> Is there some reason to use this construct rather than the list object's
>> 'append' method?
>>
>> for i in open('file.txt', 'r'):
>>      a.append(l)
> Ah, good thought. So exactly what are the functional differences
> between a list and an array in Python, or are they so close that it
> makes no difference which you use? It seems like you can index into
> both with the bracket notation. Are lists limited in any way that
> makes them not as desirable as arrays?
> <snip>

an array is of homogeneous type, so it takes much less space.  On the 
other hand, if you ever want to have some elements different from 
others, you need to use list.,  Similarly, if the type of data isn't one 
of the dozen or so defined array types, you need to use list.

DaveA



More information about the Tutor mailing list