append function problem?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • randomtalk@gmail.com

    #1

    append function problem?

    hello, recently i tried to use list.append() function in seemingly
    logical ways, however, i cannot get it to work, here is the test code:
    [color=blue][color=green][color=darkred]
    >>> seed = [2, 3, 4, 5]
    >>> next = 7
    >>> seed1 = seed.append(nex t)
    >>> seed1
    >>> print(str(seed1 ))[/color][/color][/color]
    None[color=blue][color=green][color=darkred]
    >>> def test(lst):[/color][/color][/color]
    .... print(str(lst))
    ....[color=blue][color=green][color=darkred]
    >>> test(seed.appen d(next))[/color][/color][/color]
    None

    I'm using Activestate python (latest) on win xp sp2..

    I'm not sure why seed1 and the function doesn't recognize the list..

    If anyone can point out where i'm going wrong would be much
    appreciated!
    Thanks in advance!

  • Murali

    #2
    Re: append function problem?

    A typo here? seed v/s seed1.

    Instead of "print(seed.app end(5))", try "seed.append(5) " followed by
    "print seed" -- "print(seed )" also works. The append method does not
    return the appended value
    (like many C functions).

    - Murali

    Comment

    • John Machin

      #3
      Re: append function problem?

      On 28/04/2006 1:49 PM, randomtalk@gmai l.com wrote:[color=blue]
      > hello, recently i tried to use list.append() function in seemingly
      > logical ways, however, i cannot get it to work, here is the test code:
      >[color=green][color=darkred]
      >>>> seed = [2, 3, 4, 5]
      >>>> next = 7
      >>>> seed1 = seed.append(nex t)
      >>>> seed1
      >>>> print(str(seed1 ))[/color][/color]
      > None[/color]

      (1) Try this:

      print seed

      (2) Read the documentation (especially the HEADING and the FIRST sentence):



      (3) Work your way through the Python Tutorial; here's the section on
      lists, which covers your problem:


      [color=blue][color=green][color=darkred]
      >>>> def test(lst):[/color][/color]
      > ... print(str(lst))
      > ...[color=green][color=darkred]
      >>>> test(seed.appen d(next))[/color][/color]
      > None
      >
      > I'm using Activestate python (latest) on win xp sp2..
      >
      > I'm not sure why seed1 and the function doesn't recognize the list..[/color]

      I'm not sure what "doesn't recognize the list" means.
      [color=blue]
      >
      > If anyone can point out where i'm going wrong would be much
      > appreciated!
      > Thanks in advance!
      >[/color]

      Comment

      • vbgunz

        #4
        Re: append function problem?

        seed = [1,2,3]
        seed.append(4)
        print seed # [1,2,3,4]

        many of the list methods are in place methods on a mutable object. In
        other words, doing the following results in None.

        seed = [1,2,3]
        seed = seed.append(4)
        print seed # None

        you also just wiped out your list... The append method like many other
        list methods simply return None. To get the value of an append, append
        first then access later like so.

        seed = [1,2,3]
        seed.append(4)
        print seed # [1,2,3,4]

        Comment

        • *binarystar*

          #5
          Re: append function problem?

          # Try This
          seed = [2, 3, 4, 5]
          next = [7]
          seed1 = seed + next



          randomtalk@gmai l.com wrote:[color=blue]
          > hello, recently i tried to use list.append() function in seemingly
          > logical ways, however, i cannot get it to work, here is the test code:
          >[color=green][color=darkred]
          >>>> seed = [2, 3, 4, 5]
          >>>> next = 7
          >>>> seed1 = seed.append(nex t)
          >>>> seed1
          >>>> print(str(seed1 ))[/color][/color]
          > None[color=green][color=darkred]
          >>>> def test(lst):[/color][/color]
          > ... print(str(lst))
          > ...[color=green][color=darkred]
          >>>> test(seed.appen d(next))[/color][/color]
          > None
          >
          > I'm using Activestate python (latest) on win xp sp2..
          >
          > I'm not sure why seed1 and the function doesn't recognize the list..
          >
          > If anyone can point out where i'm going wrong would be much
          > appreciated!
          > Thanks in advance!
          >[/color]

          Comment

          • bruno at modulix

            #6
            Re: append function problem?

            randomtalk@gmai l.com wrote:[color=blue]
            > hello, recently i tried to use list.append() function in seemingly
            > logical ways,[/color]

            What seems logical and how it really works may not be the same... As a
            general rule, for builtin types, destructive methods returns None. I
            personnaly find it a wart, but what, it's the BDFL's choice.

            (snip)
            [color=blue]
            > I'm not sure why seed1 and the function doesn't recognize the list..[/color]

            because list.append() modifys the list in place and returns None.


            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            Working...