[Tutor] yet another misunderstanding on my part

Dave Angel davea at davea.name
Thu Oct 23 14:58:13 CEST 2014


Dave Angel <davea at davea.name> Wrote
 in message:
> "Clayton Kirkwood" <crk at godblessthe.us> Wrote in message:
> 
> 
>  Second question, why can't a numeric index be
>> used to make assignment to a specific location like a[1] = "some value"? If
>> the mechanism is to use a.index(1,"some value"),
> 
> The index() method does not change the object a, at least not for
>  list objects. So that is not a replacement for subscripted
>  assignment. 

In particular,  the index () method searches the list.

> 
> The form a[1] = "some_value"  works fine, as long as there is
>  already an object in that list element. In other words, it can be
>  used to replace an item, but not to change the size. Python does
>  not support sparse lists, so rather than permitting a size
>  increase of exactly one, it was decided that the syntax would not
>  permit any size change. And to grow by 1, append works very
>  well.
> 
> If you know ahead of time what size you need, you could prefill it
>  with something like:
> 
>    a = [] * 20

Oops.  I meant:

      A = [None] * 20

> 
> But you might want to check later that you replaced them all. 
> 

> 
> 


-- 
DaveA



More information about the Tutor mailing list