in place-ness of list.append

BJörn Lindqvist bjourne at gmail.com
Mon Feb 5 08:44:24 EST 2007


On 2/5/07, skip at pobox.com <skip at pobox.com> wrote:
>
>     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> #--------------------------------------------------
>
> 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.

How can that be? Making a copy of a list is O(N), isn't it?

-- 
mvh Björn



More information about the Python-list mailing list