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

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

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 lan

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 tha

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 =

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 gu

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 o

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.or

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]] >>>

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,

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

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/listi

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

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 wit

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

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

inserting into a list

2006-03-07 Thread John Salerno
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