pythonlistappend

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


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?


See also: Why do these list operations (methods) return None, rather than the resulting list?


Solution

  • 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.