inserting into a list

Discussion in 'Python' started by John Salerno, Mar 7, 2006.

  1. John Salerno

    John Salerno Guest

    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? :)
    John Salerno, Mar 7, 2006
    #1
    1. Advertising

  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
    Diez B. Roggisch, Mar 7, 2006
    #2
    1. Advertising

  3. 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
    Christoph Haas, Mar 7, 2006
    #3
  4. John Salerno

    John Salerno Guest

    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]'
    John Salerno, Mar 7, 2006
    #4
  5. John Salerno

    John Salerno Guest

    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?
    John Salerno, Mar 7, 2006
    #5
  6. John Salerno

    Mel Wilson Guest

    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.
    Mel Wilson, Mar 7, 2006
    #6
  7. 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
    Diez B. Roggisch, Mar 7, 2006
    #7
  8. John Salerno

    Warby Guest

    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! :)
    Warby, Mar 7, 2006
    #8
  9. John Salerno

    John Salerno Guest

    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.
    John Salerno, Mar 7, 2006
    #9
  10. John Salerno

    John Salerno Guest

    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. :)
    John Salerno, Mar 7, 2006
    #10
  11. 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
    Diez B. Roggisch, Mar 7, 2006
    #11
  12. John Salerno

    John Salerno Guest

    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.
    John Salerno, Mar 7, 2006
    #12
  13. John Salerno

    James Stroud Guest

    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/
    James Stroud, Mar 7, 2006
    #13
  14. John Salerno

    John Salerno Guest

    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')
    John Salerno, Mar 7, 2006
    #14
  15. 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.
    Steven D'Aprano, Mar 7, 2006
    #15
  16. John Salerno

    Steve Holden Guest

    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
    Steve Holden, Mar 8, 2006
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kristian
    Replies:
    0
    Views:
    504
    Kristian
    Nov 13, 2003
  2. Jurjen de Groot

    Inserting text into TableCell at runtime

    Jurjen de Groot, Aug 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    475
    Xavier MT
    Aug 18, 2003
  3. Kevin
    Replies:
    2
    Views:
    8,881
    Kevin Spencer
    Oct 20, 2003
  4. Replies:
    0
    Views:
    387
  5. =?Utf-8?B?U3Jpbmk=?=

    Inserting into DB table with date from Generic List

    =?Utf-8?B?U3Jpbmk=?=, Nov 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    371
    =?Utf-8?B?U3Jpbmk=?=
    Nov 7, 2006
Loading...

Share This Page