How to allow list append() method to return the new list
list.append
is a built-in and therefore cannot be changed. But if you're willing to use something other than append
, you could try +
:
In [106]: myList = [10,20,30]
In [107]: yourList = myList + [40]
In [108]: print myList
[10, 20, 30]
In [109]: print yourList
[10, 20, 30, 40]
Of course, the downside to this is that a new list is created which takes a lot more time than append
Hope this helps
Don't use append but concatenation instead:
yourList = myList + [40]
This returns a new list; myList
will not be affected. If you need to have myList
affected as well either use .append()
anyway, then assign yourList
separately from (a copy of) myList
.
In python 3 you may create new list by unpacking old one and adding new element:
a = [1,2,3]
b = [*a,4] # b = [1,2,3,4]
when you do:
myList + [40]
You actually have 3 lists.