How to allow list append() method to return the new list

Wilk picture Wilk · Oct 15, 2012 · Viewed 58.9k times · Source

I want to do something like this:

myList = [10,20,30]
yourList = myList.append (40)

Unfortunately, list append does not return the modified list.

So, how can I allow append to return the new list?

Answer

Martijn Pieters picture Martijn Pieters · Oct 15, 2012

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.