473,395 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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? :)
Mar 7 '06 #1
15 9081
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
Mar 7 '06 #2
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
Mar 7 '06 #3
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]'
Mar 7 '06 #4
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?
Mar 7 '06 #5
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
Mar 7 '06 #6
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.

Mar 7 '06 #7
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! :)

Mar 7 '06 #8
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.
Mar 7 '06 #9
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. :)
Mar 7 '06 #10
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
Mar 7 '06 #11
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.
Mar 7 '06 #12
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/
Mar 7 '06 #13
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')
Mar 7 '06 #14
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.

Mar 7 '06 #15
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

Mar 8 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Moss | last post by:
I have (almost) succesfully created a linked list data structure class. It will quite happily add items to to the end of the list (once I found that objects are not passed by reference :). What I...
1
by: pmud | last post by:
I have an ASP.NET web application using C# code. I am trying to insert values from a web form into an SQL database. I am using SQL COMMAND object for this. I need to know HOW TO INSERT THE RADIO...
1
by: Scott Chapman | last post by:
I am working with Python (psycopg). I have HTML with embedded Python that I'm inserting into a database and it could contain any character. Single quotes, at least, must be escaped (to two...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
1
by: Sailer, Denis | last post by:
There was a posting in the mailing list archives that I can't find anymore. The web site right now is presenting a list of items from a search in a reasonable amount of time, but takes 5-10 minutes...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
14
Parul Bagadia
by: Parul Bagadia | last post by:
Here is the code i hav written for inserting a no., after given no. in a link list; i guess the logic is ofcourse right. there is no error in it, but at the time of display its not displaying the...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: Edwin.Madari | last post by:
-----Original Message----- statement prepared first and executed many times with exectemany - db API http://www.python.org/dev/peps/pep-0249/ inline statemets can be exeucuted only. hope that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.