in place-ness of list.append
Kent Johnson
kent at kentsjohnson.com
Mon Feb 5 06:00:50 EST 2007
Bart Van Loon wrote:
> Hi all,
>
> I would like to find out of a good way to append an element to a list
> without chaing that list in place, like the builtin list.append() does.
>
> currently, I am using the following (for a list of integers, but it
> could be anything, really)
>
> #--------------------------------------------------
> def addnumber(alist, num):
> """ work around the inplace-ness of .append """
> mylist = alist[:]
> mylist.append(num)
> return mylist
> #--------------------------------------------------
Use + :
In [1]: a=[1,2]
In [2]: b=a+[3]
In [3]: a
Out[3]: [1, 2]
In [4]: b
Out[4]: [1, 2, 3]
Kent
More information about the Python-list
mailing list