in place-ness of list.append

skip at pobox.com skip at pobox.com
Mon Feb 5 06:01:28 EST 2007


    Bart> #--------------------------------------------------
    Bart> def addnumber(alist, num):
    Bart>     """ work around the inplace-ness of .append """ 
    Bart>     mylist = alist[:]
    Bart>     mylist.append(num)
    Bart>     return mylist
    Bart> #--------------------------------------------------

    Bart> and I am wondering if this is good practice or not.

    Bart> any advice on this matter?

Such an operation will be O(N**2), and thus expensive if performed
frequently on lists of moderate length.  I've never been tempted to do this.
Can you say a little about why you'd want to do this?  Knowing that, perhaps
someone here can point you in a more Pythonic direction.

Skip



More information about the Python-list mailing list