Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > inserting into a list

Reply
Thread Tools

inserting into a list

 
 
John Salerno
Guest
Posts: n/a
 
      03-07-2006
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?
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-07-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
 
Reply With Quote
 
 
 
 
Christoph Haas
Guest
Posts: n/a
 
      03-07-2006
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
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-07-2006
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]'
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-07-2006
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?
 
Reply With Quote
 
Mel Wilson
Guest
Posts: n/a
 
      03-07-2006
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.

 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      03-07-2006
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
 
Reply With Quote
 
Warby
Guest
Posts: n/a
 
      03-07-2006
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!

 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-07-2006
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.
 
Reply With Quote
 
John Salerno
Guest
Posts: n/a
 
      03-07-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting into DB table with date from Generic List =?Utf-8?B?U3Jpbmk=?= ASP .Net 0 11-07-2006 04:33 AM
DBI problem inserting html into mssql db Kristian Perl 0 11-13-2003 12:23 PM
Retrieve ID after inserting into Access database refer_to_website@nospam.com ASP .Net 0 10-28-2003 10:07 PM
inserting into MS access using ASP.NET/VB Kevin ASP .Net 2 10-20-2003 12:01 PM
Inserting text into TableCell at runtime Jurjen de Groot ASP .Net 3 08-18-2003 09:38 PM



Advertisments