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

前端 未结 7 1584
春和景丽
春和景丽 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:16

    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

提交回复
热议问题