List Behavior when inserting new items
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Mon Jan 29 16:26:28 EST 2007
Drew a écrit :
> I'm looking to add an element to list of items, however I'd like to
> add it at a specific index greater than the current size:
>
> list = [1,2,3]
NB: better to avoid using builtins types and functions names as identifiers.
> list.insert(10,4)
>
> What I'd like to see is something like:
>
> [1,2,3,,,,,,4]
Hint : the Python representation of nothing is a singleton object named
None.
> However I see:
>
> [1,2,3,4]
Yeps. I thought it would have raised an IndexError. But I seldom use
list.insert() to append to a list - there's list.append() (and/or
list.extend()) for this.
> Is there any way to produce this kind of behavior easily?
Hints:
>>> [None] * 5
[None, None, None, None, None]
>>> [1, 2, 3, None] + [10]
[1, 2, 3, None, 10]
HTH
More information about the Python-list
mailing list