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

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

    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.

    0 讨论(0)
提交回复
热议问题