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

前端 未结 7 1598
春和景丽
春和景丽 2020-12-09 00:31

I want to do something like this:

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

Unfortunately, list append does not return the modified

7条回答
  •  再見小時候
    2020-12-09 01:03

    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.

提交回复
热议问题