Re: inserting into a list

2006-03-07 Thread Christoph Haas
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Ratko Jagodic
from the Library Reference:s.insert(i, x)
same as s[i:i] = [x]
(5)On 3/7/06, John Salerno [EMAIL PROTECTED]
 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 workeither.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 thelist reads [1, 2, 3, 4]I've tried all kinds of combinations of slicing assignment, but I alwaysget:
TypeError: can only assign an iterableCan someone please embarrass me with the simple answer?:)--http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: inserting into a list

2006-03-07 Thread John Salerno
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]'
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread John Salerno
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?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Diez B. Roggisch
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Mel Wilson
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Warby
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! :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread John Salerno
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Diez B. Roggisch
John Salerno wrote:

 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.

You got an explanation to your first question from me.

But you obviously _didn't_ try your second one. Which would be a no-brainer
as Warby's reply shows, and given that you already knew how to insert a
single element list I consider it being not so nice of _you_ not to do so,
but instead post before you tried.

Diez
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread John Salerno
Diez B. Roggisch wrote:
 John Salerno wrote:
 
 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.
 
 You got an explanation to your first question from me.
 
 But you obviously _didn't_ try your second one. Which would be a no-brainer
 as Warby's reply shows, and given that you already knew how to insert a
 single element list I consider it being not so nice of _you_ not to do so,
 but instead post before you tried.
 
 Diez

Actually, I did try the second one too, but I'm not trying to get into a 
fight here, so let's just forget it. I appreciate the fact that you 
responded at all, and with answers to my questions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread John Salerno
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.  :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread James Stroud
John Salerno wrote:
 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.

People who answer questions on this list have forgotten how unintuitive 
intuitive can be. In other words, they have found that the intuitive way 
to do things in python is usually the right way, which may not be the 
case in other languages. Thus, your instinct to see if your instincts 
are correct rings as laziness here, when in fact you are just being 
rigorous.

Here is one my favorite examples of python intuitiveness:

if something is not something_else:
   do_whatever()

Who would have thunk it?

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread John Salerno
James Stroud wrote:

 Here is one my favorite examples of python intuitiveness:
 
 if something is not something_else:
   do_whatever()
 
 Who would have thunk it?

That's actually one of the things that first surprised me about Python, 
that you can actually say is not in a programming language, and it 
reads like a sentence!  :)  (Not to mention 'and' and 'or')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Steven D'Aprano
On Tue, 07 Mar 2006 12:26:00 -0800, James Stroud wrote:

 John Salerno wrote:
 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.
 
 People who answer questions on this list have forgotten how unintuitive 
 intuitive can be. In other words, they have found that the intuitive way 
 to do things in python is usually the right way, which may not be the 
 case in other languages. Thus, your instinct to see if your instincts 
 are correct rings as laziness here, when in fact you are just being 
 rigorous.

Not rigorous. Perhaps thorougher.

Had the OP worded the question more rigorously, we wouldn't be having this
argument:

I wanted to see what happens if you try to insert a list into a list
using slicing, and discovered that this works:

L[2:2] = [ [1,2,3] ]

Now I don't understand the reasoning behind this. Can somebody explain the
rationale between needing to wrap objects in a list in order to insert
using slices?

To which the answer would be, so it is consistent with other slice
assignment:

L[2:10] = [1, 2, 3, 4]

Retrieving a slice returns a list. Assigning to a slice requires a list.
Making an exception for the special case of L[x:x] goes against
the philosophy of the Python language: Python generally doesn't accept
that special cases are special enough to break the rules.

Half the battle is asking the right question. The other half of the battle
is asking the right question in the right way.



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: inserting into a list

2006-03-07 Thread Steve Holden
Steven D'Aprano wrote:
 On Tue, 07 Mar 2006 12:26:00 -0800, James Stroud wrote:
 
 
John Salerno wrote:

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.

People who answer questions on this list have forgotten how unintuitive 
intuitive can be. In other words, they have found that the intuitive way 
to do things in python is usually the right way, which may not be the 
case in other languages. Thus, your instinct to see if your instincts 
are correct rings as laziness here, when in fact you are just being 
rigorous.
 
 
 Not rigorous. Perhaps thorougher.
 
 Had the OP worded the question more rigorously, we wouldn't be having this
 argument:
 
 I wanted to see what happens if you try to insert a list into a list
 using slicing, and discovered that this works:
 
 L[2:2] = [ [1,2,3] ]
 
 Now I don't understand the reasoning behind this. Can somebody explain the
 rationale between needing to wrap objects in a list in order to insert
 using slices?
 
 To which the answer would be, so it is consistent with other slice
 assignment:
 
 L[2:10] = [1, 2, 3, 4]
 
 Retrieving a slice returns a list. Assigning to a slice requires a list.
 Making an exception for the special case of L[x:x] goes against
 the philosophy of the Python language: Python generally doesn't accept
 that special cases are special enough to break the rules.
 
 Half the battle is asking the right question. The other half of the battle
 is asking the right question in the right way.
 
 
 
And the third half of the battle is focusing on keeping everybody moving 
forward, approximately together. Let's move on now, nothing to see here ;-)

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

-- 
http://mail.python.org/mailman/listinfo/python-list