#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0

    Which is better append or a[len(a):] = [x] ?


    I know both are equivalent in term of effect but is one or the other better in terms of standards, speed or any other considerations ?
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Use append . Besides being the fastest aList.append() is also much easier on the eyes and makes it far more obvious what your code is doing.

    Heres a quick benchmark for the two expressions using the timeit module (see the "Tips, Tricks, Inspiration etc." thread at the top of the forum).

    Code:
    >>> import timeit
    >>>       
    >>> a = []
    >>> timeit.Timer('a[len(a):] = [0]', 'from __main__ import a').timeit()
    2.5353608131408691
    >>> 
    >>> b = []
    >>> timeit.Timer('b.append(0)', 'from __main__ import b').timeit()
    1.2892959117889404
    >>>
    Note: this was done using Python 2.4 under Mac OS X 10.3.8 on a 1Ghz G4 iBook with 1GB RAM.

    Enjoy,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    In my personal opinion, I think that append would be better, but, thats just me
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    20
    Rep Power
    0
    It seems there is no reason not to use append

    The only thing I could see why I would use the slice operator is to get used to it ...

    So I am going to stick with append
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    78
    Rep Power
    10
    You'd use the append method here as it's simpler. The append method is generally better for this, as you
    can assign it to a variable (Python has higher order functions) or pass it in as a function parameter, eg:
    Code:
    # Really bad way of building a list of integers - for demo purposes only!
    def compose(f, values):
        for x in values:
            f(x)
    
    mylist = []
    f = mylist.append
    values = range(10)
    compose(f, values)
    print mylist
    Slicing is useful elsewhere, for instance if you want to remove a chunk of mylist:
    Code:
    del mylist[4:6]
    print mylist
    Or make a shallow copy:
    Code:
    mylist2 = mylist[:]
    print mylist2
    Or insert another list:
    Code:
    mylist[4:4] = range(10,20)  
    print mylist
    Note that this is different from the insert method:
    Code:
    mylist2.insert(4, range(10,20))
    print mylist2
    Note also that there is an alternative to the append method:
    Code:
    mylist = range(10)
    mylist += range(11,20)
    print mylist
    --OH.

IMN logo majestic logo threadwatch logo seochat tools logo