inserting into a list

J

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]

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? :)
 
D

Diez B. Roggisch

John said:
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
 
C

Christoph Haas

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
 
J

John Salerno

Diez said:
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]'
 
J

John Salerno

Christoph said:
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?
 
M

Mel Wilson

John said:
Christoph said:
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.
 
D

Diez B. Roggisch

John said:
Christoph said:
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
 
J

John Salerno

Diez said:
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.
 
J

John Salerno

Warby said:
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. :)
 
D

Diez B. Roggisch

John said:
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
 
J

John Salerno

Diez said:
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.
 
J

James Stroud

John said:
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/
 
J

John Salerno

James said:
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')
 
S

Steven D'Aprano

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

Steve Holden

Steven said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top