inserting into a list

Diez B. Roggisch deets at nospam.web.de
Tue Mar 7 10:31:35 EST 2006


John Salerno wrote:

> Let me apologize in advance for what I'm sure is an achingly simple
> question, but I just can't find the answer in either of my Python books.
> I've tried a few tests with the interactive prompt, but they don't work
> either.
> 
> All I'm trying to do is insert an item into a list, like so:
> 
> L = [1, 2, 4]
> 
> and I want to insert the integer 3 into the position L[2], so that the
> list reads [1, 2, 3, 4]
> 
> I've tried all kinds of combinations of slicing assignment, but I always
> get:
> 
> TypeError: can only assign an iterable
> 
> Can someone please embarrass me with the simple answer?  :)

>>> l = [1,2,3]
>>> l.insert(2, 10)
>>> l
[1, 2, 10, 3]
>>>

Embarrasing enough?

Diez



More information about the Python-list mailing list