inserting into a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    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? :)
  • Christoph Haas

    #2
    Re: inserting into a list

    On Tuesday 07 March 2006 16:18, John Salerno wrote:[color=blue]
    > 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][/color]

    Either

    L[2:2]=[3]

    or

    L.insert(2,3)

    Kindly
    Christoph
    --
    ~
    ~
    ".signature " [Modified] 1 line --100%-- 1,48 All

    Comment

    • Diez B. Roggisch

      #3
      Re: inserting into a list

      John Salerno wrote:
      [color=blue]
      > 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? :)[/color]
      [color=blue][color=green][color=darkred]
      >>> l = [1,2,3]
      >>> l.insert(2, 10)
      >>> l[/color][/color][/color]
      [1, 2, 10, 3][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Embarrasing enough?

      Diez

      Comment

      • John Salerno

        #4
        Re: inserting into a list

        Diez B. Roggisch wrote:
        [color=blue][color=green][color=darkred]
        >>>> l = [1,2,3]
        >>>> l.insert(2, 10)
        >>>> l[/color][/color]
        > [1, 2, 10, 3]
        >
        > Embarrasing enough?[/color]

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

        Comment

        • John Salerno

          #5
          Re: inserting into a list

          Christoph Haas wrote:
          [color=blue]
          > L[2:2]=[3][/color]

          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?

          Comment

          • Diez B. Roggisch

            #6
            Re: inserting into a list

            John Salerno wrote:
            [color=blue]
            > Christoph Haas wrote:
            >[color=green]
            >> L[2:2]=[3][/color]
            >
            > 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?[/color]

            Because a slice represents a list - even if it is a one-elemented one. So,
            replacing it you need another list.
            [color=blue]
            > What if you wanted to insert an actual list into that slot? Would
            > you have to wrap it in double brackets?[/color]

            Why don't you just _try_ that? It would have been way faster than to ask
            questions you can easily answer yourself.

            Diez

            Comment

            • Mel Wilson

              #7
              Re: inserting into a list

              John Salerno wrote:[color=blue]
              > Christoph Haas wrote:[color=green]
              >> L[2:2]=[3][/color][/color]
              [ ... ]
              What if you wanted to insert an actual list into that
              slot? Would[color=blue]
              > you have to wrap it in double brackets?[/color]

              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.[color=blue][color=green][color=darkred]
              >>> a=[1,3,4]
              >>> a[2:3][/color][/color][/color]
              [4][color=blue][color=green][color=darkred]
              >>> a[2:2][/color][/color][/color]
              [][color=blue][color=green][color=darkred]
              >>> a[1:1]=[2]
              >>> a[/color][/color][/color]
              [1, 2, 3, 4][color=blue][color=green][color=darkred]
              >>> a[1:2][/color][/color][/color]
              [2]


              Mel.

              Comment

              • Warby

                #8
                Re: inserting into a list

                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:
                [color=blue][color=green][color=darkred]
                >>> L = [1,2,4]
                >>> print L[2:2][/color][/color][/color]
                []

                As for your question, yes:
                [color=blue][color=green][color=darkred]
                >>> L = [1,2,4]
                >>> L[2:2] = [[3]]
                >>> print L[/color][/color][/color]
                [1, 2, [3], 4]

                Cheers! :)

                Comment

                • John Salerno

                  #9
                  Re: inserting into a list

                  Diez B. Roggisch wrote:
                  [color=blue]
                  > Why don't you just _try_ that? It would have been way faster than to ask
                  > questions you can easily answer yourself.[/color]

                  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.

                  Comment

                  • John Salerno

                    #10
                    Re: inserting into a list

                    Warby wrote:[color=blue]
                    > 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:
                    >[color=green][color=darkred]
                    >>>> L = [1,2,4]
                    >>>> print L[2:2][/color][/color]
                    > []
                    >
                    > As for your question, yes:
                    >[color=green][color=darkred]
                    >>>> L = [1,2,4]
                    >>>> L[2:2] = [[3]]
                    >>>> print L[/color][/color]
                    > [1, 2, [3], 4]
                    >
                    > Cheers! :)
                    >[/color]

                    Thanks guys! What I wasn't realizing was that a slice is a list, so I
                    needed a list. :)

                    Comment

                    • Diez B. Roggisch

                      #11
                      Re: inserting into a list

                      John Salerno wrote:
                      [color=blue]
                      > Diez B. Roggisch wrote:
                      >[color=green]
                      >> Why don't you just _try_ that? It would have been way faster than to ask
                      >> questions you can easily answer yourself.[/color]
                      >
                      > 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.[/color]

                      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

                      Comment

                      • John Salerno

                        #12
                        Re: inserting into a list

                        Diez B. Roggisch wrote:[color=blue]
                        > John Salerno wrote:
                        >[color=green]
                        >> Diez B. Roggisch wrote:
                        >>[color=darkred]
                        >>> Why don't you just _try_ that? It would have been way faster than to ask
                        >>> questions you can easily answer yourself.[/color]
                        >> 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.[/color]
                        >
                        > 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[/color]

                        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.

                        Comment

                        • James Stroud

                          #13
                          Re: inserting into a list

                          John Salerno wrote:[color=blue]
                          > Diez B. Roggisch wrote:
                          >[color=green]
                          >> Why don't you just _try_ that? It would have been way faster than to ask
                          >> questions you can easily answer yourself.[/color]
                          >
                          >
                          > 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.[/color]

                          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


                          Comment

                          • John Salerno

                            #14
                            Re: inserting into a list

                            James Stroud wrote:
                            [color=blue]
                            > Here is one my favorite examples of python intuitiveness:
                            >
                            > if something is not something_else:
                            > do_whatever()
                            >
                            > Who would have thunk it?[/color]

                            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')

                            Comment

                            • Steven D'Aprano

                              #15
                              Re: inserting into a list

                              On Tue, 07 Mar 2006 12:26:00 -0800, James Stroud wrote:
                              [color=blue]
                              > John Salerno wrote:[color=green]
                              >> Diez B. Roggisch wrote:
                              >>[color=darkred]
                              >>> Why don't you just _try_ that? It would have been way faster than to ask
                              >>> questions you can easily answer yourself.[/color]
                              >>
                              >>
                              >> 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.[/color]
                              >
                              > 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.[/color]

                              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.

                              Comment

                              Working...