in place-ness of list.append

Bart Van Loon bbbart at inGen.be
Mon Feb 5 06:08:17 EST 2007


It was Mon, 05 Feb 2007 11:00:50 GMT, when Kent Johnson wrote:
> 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]

should have known that...

thanks for you fast response!

-- 
regards,
BBBart

   "Who can fathom the feminine mind?" -Calvin "I like `em anyway" -Hobbes



More information about the Python-list mailing list