Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   inserting into a list (http://www.velocityreviews.com/forums/t355192-inserting-into-a-list.html)

John Salerno 03-07-2006 03:18 PM

inserting into a list
 
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? :)

Diez B. Roggisch 03-07-2006 03:31 PM

Re: inserting into a list
 
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

Christoph Haas 03-07-2006 03:32 PM

Re: inserting into a list
 
On Tuesday 07 March 2006 16:18, 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]


Either

L[2:2]=[3]

or

L.insert(2,3)

Kindly
Christoph
--
~
~
".signature" [Modified] 1 line --100%-- 1,48 All

John Salerno 03-07-2006 04:01 PM

Re: inserting into a list
 
Diez B. Roggisch wrote:

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

> [1, 2, 10, 3]
>
> Embarrasing enough?


Actually, I was trying to figure it out with the slice technique
instead. But yeah, as Christopher's example showed, it's not hard. But I
didn't realize you had to assign a list item to the slice, so I was doing:

L[2:2] = 3

among other things, but they all involved '= 3', not '= [3]'

John Salerno 03-07-2006 04:02 PM

Re: inserting into a list
 
Christoph Haas wrote:

> L[2:2]=[3]


I'm still a little confused about this. If what I'm inserting is just an
integer, why wouldn't

L[2:2] = 3

work? What if you wanted to insert an actual list into that slot? Would
you have to wrap it in double brackets?

Mel Wilson 03-07-2006 04:24 PM

Re: inserting into a list
 
John Salerno wrote:
> Christoph Haas wrote:
>> L[2:2]=[3]

[ ... ]
What if you wanted to insert an actual list into that
slot? Would
> you have to wrap it in double brackets?


Yep.

It's a strong-typing thing. Slices of lists are lists, and
therefore what you assign to one has got to be a list, or
convertible to a list (a tuple would work.)

Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more
information.
>>> a=[1,3,4]
>>> a[2:3]

[4]
>>> a[2:2]

[]
>>> a[1:1]=[2]
>>> a

[1, 2, 3, 4]
>>> a[1:2]

[2]


Mel.


Diez B. Roggisch 03-07-2006 04:30 PM

Re: inserting into a list
 
John Salerno wrote:

> Christoph Haas wrote:
>
>> L[2:2]=[3]

>
> I'm still a little confused about this. If what I'm inserting is just an
> integer, why wouldn't
> L[2:2] = 3
>
> work?


Because a slice represents a list - even if it is a one-elemented one. So,
replacing it you need another list.

> What if you wanted to insert an actual list into that slot? Would
> you have to wrap it in double brackets?


Why don't you just _try_ that? It would have been way faster than to ask
questions you can easily answer yourself.

Diez

Warby 03-07-2006 04:41 PM

Re: inserting into a list
 
It makes sense because a slice IS a list, so you should assign a list
to it. Yours is just a special case in which the target slice has a
length of zero. It's still a list, just an empty one:

>>> L = [1,2,4]
>>> print L[2:2]

[]

As for your question, yes:

>>> L = [1,2,4]
>>> L[2:2] = [[3]]
>>> print L

[1, 2, [3], 4]

Cheers! :)


John Salerno 03-07-2006 04:44 PM

Re: inserting into a list
 
Diez B. Roggisch wrote:

> Why don't you just _try_ that? It would have been way faster than to ask
> questions you can easily answer yourself.


I did try it, but I was still hoping for an explanation, which I've also
gotten from you guys, some in nicer terms than others.

John Salerno 03-07-2006 04:45 PM

Re: inserting into a list
 
Warby wrote:
> It makes sense because a slice IS a list, so you should assign a list
> to it. Yours is just a special case in which the target slice has a
> length of zero. It's still a list, just an empty one:
>
>>>> L = [1,2,4]
>>>> print L[2:2]

> []
>
> As for your question, yes:
>
>>>> L = [1,2,4]
>>>> L[2:2] = [[3]]
>>>> print L

> [1, 2, [3], 4]
>
> Cheers! :)
>


Thanks guys! What I wasn't realizing was that a slice is a list, so I
needed a list. :)


All times are GMT. The time now is 09:58 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.