append on lists

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

    append on lists



    Hi,

    just a dumb question.

    Let a = [1,2,3,4,5]

    Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

    --Armin
  • Fredrik Lundh

    #2
    Re: append on lists

    Armin wrote:
    just a dumb question.
    >
    Let a = [1,2,3,4,5]
    >
    Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
    yeah, that's a dumb question.

    </F>

    Comment

    • Armin

      #3
      Re: append on lists

      Fredrik Lundh wrote:
      Armin wrote:
      >
      >just a dumb question.
      >>
      >Let a = [1,2,3,4,5]
      >>
      >Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
      >
      yeah, that's a dumb question.
      >
      </F>
      >
      yeah, that's a dumb answer.

      Comment

      • Chris Rebert

        #4
        Re: append on lists

        On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
        >
        >
        Hi,
        >
        just a dumb question.
        >
        Let a = [1,2,3,4,5]
        >
        Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
        I'll assume the presence of the 6 is a typo.

        Because .append() mutates 'a' and appends the item in-place rather
        than creating and returning a new list with the item appended, and
        it's good Python style for mutating methods to have no return value
        (since all functions must have some return value, Python uses None
        when the function doesn't explicitly return anything).

        If you print 'a' after doing the .append(), you'll see it's changed to
        your desired value.

        Regards,
        Chris
        --
        Follow the path of the Iguana...

        Comment

        • Arnaud Delobelle

          #5
          Re: append on lists

          On Sep 15, 9:24 pm, Armin <a...@nospam.or gwrote:
          Hi,
          >
          just a dumb question.
          >
          Let a = [1,2,3,4,5]
          >
          Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
          >
          --Armin
          Because list.append is a method that mutates its object, and such
          method usually return None. What you should check is the value of 'a'
          after 'a.append(7)'.

          --
          Arnaud

          Comment

          • Jerry Hill

            #6
            Re: append on lists

            On Mon, Sep 15, 2008 at 4:24 PM, Armin <a@nospam.orgwr ote:
            Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
            Because the list a has been altered in place.

            --
            Jerry

            Comment

            • Armin

              #7
              Re: append on lists

              Chris Rebert wrote:
              On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
              >>
              >Hi,
              >>
              >just a dumb question.
              >>
              >Let a = [1,2,3,4,5]
              >>
              >Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
              I'll assume the presence of the 6 is a typo.
              Sorry, that's the case.
              >
              Because .append() mutates 'a' and appends the item in-place rather
              than creating and returning a new list with the item appended, and
              it's good Python style for mutating methods to have no return value
              (since all functions must have some return value, Python uses None
              when the function doesn't explicitly return anything).
              Yes, but this is very unconvenient.
              If d should reference the list a extended with a single list element
              you need at least two lines

              a.append(7)
              d=a

              and not more intuitive d = a.append(7)

              --Armin

              >
              If you print 'a' after doing the .append(), you'll see it's changed to
              your desired value.
              >
              Regards,
              Chris
              >

              Comment

              • Fredrik Lundh

                #8
                Re: append on lists

                Armin wrote:
                >>just a dumb question.
                >>>
                >>Let a = [1,2,3,4,5]
                >>>
                >>Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
                >>
                >yeah, that's a dumb question.
                >
                yeah, that's a dumb answer.
                did you read your own post? I did.

                </F>

                Comment

                • Grant Edwards

                  #9
                  Re: append on lists

                  On 2008-09-15, Armin <a@nospam.orgwr ote:
                  >Because .append() mutates 'a' and appends the item in-place rather
                  >than creating and returning a new list with the item appended, and
                  >it's good Python style for mutating methods to have no return value
                  >(since all functions must have some return value, Python uses None
                  >when the function doesn't explicitly return anything).
                  >
                  Yes, but this is very unconvenient.
                  Aw, admit it -- it's not _that_ inconvenient.
                  If d should reference the list a extended with a single list
                  element you need at least two lines
                  >
                  a.append(7)
                  d=a
                  With that usage it's obvious that a is mutated, and now both
                  "a" and "d" are bound to the same extended list.
                  and not more intuitive d = a.append(7)
                  Becase that usage implies (at least to many of us) that "a" is
                  unchanged, and that "d" is now bound to a different object than
                  "a".

                  --
                  Grant Edwards grante Yow! Where's th' DAFFY
                  at DUCK EXHIBIT??
                  visi.com

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: append on lists

                    Armin wrote:
                    If d should reference the list a extended with a single list element
                    you need at least two lines
                    >
                    a.append(7)
                    d=a
                    why do you need two names for the same thing?
                    and not more intuitive d = a.append(7)
                    unless you completely change the semantics of "append", your code would
                    modify "a" as well. how is that "more intuitive"?

                    side effects are bad as they are, but side effects in unexpected places
                    is a really lousy idea. I don't think you've thought this one through,
                    really.

                    </F>

                    Comment

                    • John Machin

                      #11
                      Re: append on lists

                      On Sep 16, 6:45 am, Armin <a...@nospam.or gwrote:
                      Yes, but this is very unconvenient.
                      If d should reference the list a extended with a single list element
                      you need at least two lines
                      >
                      a.append(7)
                      d=a
                      >
                      and not more intuitive d = a.append(7)
                      Methods/functions which return a value other than the formal None and
                      also mutate their environment are "a snare and a delusion". Don't wish
                      for them.

                      Inconvenient? How often do you want to mutate a list and then set up
                      another reference to it?

                      Comment

                      • Chris Rebert

                        #12
                        Re: append on lists

                        On Mon, Sep 15, 2008 at 1:45 PM, Armin <a@nospam.orgwr ote:
                        Chris Rebert wrote:
                        >>
                        >On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
                        >>>
                        >>Hi,
                        >>>
                        >>just a dumb question.
                        >>>
                        >>Let a = [1,2,3,4,5]
                        >>>
                        >>Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
                        >>
                        >I'll assume the presence of the 6 is a typo.
                        >
                        Sorry, that's the case.
                        >
                        >>
                        >Because .append() mutates 'a' and appends the item in-place rather
                        >than creating and returning a new list with the item appended, and
                        >it's good Python style for mutating methods to have no return value
                        >(since all functions must have some return value, Python uses None
                        >when the function doesn't explicitly return anything).
                        >
                        Yes, but this is very unconvenient.
                        If d should reference the list a extended with a single list element
                        you need at least two lines
                        >
                        a.append(7)
                        d=a
                        >
                        and not more intuitive d = a.append(7)
                        And then they'd both reference the same list and you'd run into all
                        sorts of problems.

                        The code you'd actually want is:

                        d = a[:] #copy a
                        d.append(7)

                        Or if you're willing to overlook the inefficiency:

                        d = a + [7]

                        But that's not idiomatic.

                        And debating the fundamentals of the language, which aren't going to
                        change anytime soon, isn't going to get you anywhere.
                        You may be interested in looking at Python's "tuple" datatype, which
                        is basically an immutable list.

                        I'd also suggest you Read The Fine Tutorial, and that your original
                        question was better suited to IRC or python-tutors
                        (http://mail.python.org/mailman/listinfo/tutor) than this mailinglist.

                        Regards,
                        Chris
                        >
                        --Armin
                        >
                        >
                        >>
                        >If you print 'a' after doing the .append(), you'll see it's changed to
                        >your desired value.
                        >
                        >
                        >>
                        >Regards,
                        >Chris
                        >>
                        --

                        >


                        --
                        Follow the path of the Iguana...

                        Comment

                        • Jerry Hill

                          #13
                          Re: append on lists

                          On Mon, Sep 15, 2008 at 4:45 PM, Armin <a@nospam.orgwr ote:
                          Yes, but this is very unconvenient.
                          If d should reference the list a extended with a single list element
                          you need at least two lines
                          You do it in two lines, because you're doing two different things.
                          a.append(7)
                          This appends the element 7 to the list a.
                          d=a
                          This binds the name d to the same list as a is bound to. If you wand
                          d to point to a new list with the same contents as the list a, plus
                          the number 7 do this:

                          d = a + [7]

                          Here's an example of the difference:
                          >>a = range(6)
                          >>a
                          [0, 1, 2, 3, 4, 5]
                          >>a.append(7)
                          >>a
                          [0, 1, 2, 3, 4, 5, 7]
                          >>d = a
                          >>d
                          [0, 1, 2, 3, 4, 5, 7]
                          >>d.append(10 )
                          >>a
                          [0, 1, 2, 3, 4, 5, 7, 10]
                          >>d
                          [0, 1, 2, 3, 4, 5, 7, 10]
                          >>>
                          See how a and d are two names bound to the same list?

                          Here's the other way:
                          >>a = range(6)
                          >>d = a + [7]
                          >>a
                          [0, 1, 2, 3, 4, 5]
                          >>d
                          [0, 1, 2, 3, 4, 5, 7]
                          >>d.append(10 )
                          >>a
                          [0, 1, 2, 3, 4, 5]
                          >>d
                          [0, 1, 2, 3, 4, 5, 7, 10]
                          >>>
                          --
                          Jerry

                          Comment

                          • Steven D'Aprano

                            #14
                            Re: append on lists

                            On Mon, 15 Sep 2008 13:47:53 -0700, Chris Rebert wrote:
                            The code you'd actually want is:
                            >
                            d = a[:] #copy a
                            d.append(7)
                            >
                            Or if you're willing to overlook the inefficiency:
                            >
                            d = a + [7]
                            >
                            But that's not idiomatic.
                            Why is a + [7] more inefficient than manually copying the list and
                            appending to the copy? Surely both pieces of code end up doing the same
                            thing?

                            In fact, I'd guess that the second is likely to be marginally more
                            efficient than the first:

                            >>x = compile('d = a[:]; d.append(7)', '', 'exec')
                            >>dis.dis(x)
                            1 0 LOAD_NAME 0 (a)
                            3 SLICE+0
                            4 STORE_NAME 1 (d)
                            7 LOAD_NAME 1 (d)
                            10 LOAD_ATTR 2 (append)
                            13 LOAD_CONST 0 (7)
                            16 CALL_FUNCTION 1
                            19 POP_TOP
                            20 LOAD_CONST 1 (None)
                            23 RETURN_VALUE
                            >>x = compile('d = a + [7]', '', 'exec')
                            >>dis.dis(x)
                            1 0 LOAD_NAME 0 (a)
                            3 LOAD_CONST 0 (7)
                            6 BUILD_LIST 1
                            9 BINARY_ADD
                            10 STORE_NAME 1 (d)
                            13 LOAD_CONST 1 (None)
                            16 RETURN_VALUE


                            timeit agrees with me:
                            >>from timeit import Timer
                            >>t1 = Timer('d = a[:]; d.append(7)', 'a = []')
                            >>t2 = Timer('d = a + [7]', 'a = []')
                            >>t1.repeat(num ber=1000)
                            [0.0015339851379 394531, 0.0014910697937 011719, 0.0014841556549 072266]
                            >>t2.repeat(num ber=1000)
                            [0.0011889934539 794922, 0.0013048648834 228516, 0.0013070106506 347656]


                            --
                            Steven

                            Comment

                            • Chris Rebert

                              #15
                              Re: append on lists

                              On Mon, Sep 15, 2008 at 4:03 PM, Steven D'Aprano
                              <steve@remove-this-cybersource.com .auwrote:
                              On Mon, 15 Sep 2008 13:47:53 -0700, Chris Rebert wrote:
                              >
                              >The code you'd actually want is:
                              >>
                              >d = a[:] #copy a
                              >d.append(7)
                              >>
                              >Or if you're willing to overlook the inefficiency:
                              >>
                              >d = a + [7]
                              >>
                              >But that's not idiomatic.
                              >
                              Why is a + [7] more inefficient than manually copying the list and
                              appending to the copy? Surely both pieces of code end up doing the same
                              thing?
                              >
                              In fact, I'd guess that the second is likely to be marginally more
                              efficient than the first:
                              >
                              >
                              >>>x = compile('d = a[:]; d.append(7)', '', 'exec')
                              >>>dis.dis(x)
                              1 0 LOAD_NAME 0 (a)
                              3 SLICE+0
                              4 STORE_NAME 1 (d)
                              7 LOAD_NAME 1 (d)
                              10 LOAD_ATTR 2 (append)
                              13 LOAD_CONST 0 (7)
                              16 CALL_FUNCTION 1
                              19 POP_TOP
                              20 LOAD_CONST 1 (None)
                              23 RETURN_VALUE
                              >
                              >>>x = compile('d = a + [7]', '', 'exec')
                              >>>dis.dis(x)
                              1 0 LOAD_NAME 0 (a)
                              3 LOAD_CONST 0 (7)
                              6 BUILD_LIST 1
                              9 BINARY_ADD
                              10 STORE_NAME 1 (d)
                              13 LOAD_CONST 1 (None)
                              16 RETURN_VALUE
                              >
                              >
                              timeit agrees with me:
                              >
                              >>>from timeit import Timer
                              >>>t1 = Timer('d = a[:]; d.append(7)', 'a = []')
                              >>>t2 = Timer('d = a + [7]', 'a = []')
                              >>>t1.repeat(nu mber=1000)
                              [0.0015339851379 394531, 0.0014910697937 011719, 0.0014841556549 072266]
                              >>>t2.repeat(nu mber=1000)
                              [0.0011889934539 794922, 0.0013048648834 228516, 0.0013070106506 347656]
                              >
                              >
                              --
                              Steven
                              --

                              >
                              Sorry, I was just speculating based on the extraneous list ([7]) used
                              in the second one.
                              My bad. :)

                              Regards,
                              Chris

                              --
                              Follow the path of the Iguana...

                              Comment

                              Working...