How to fill a list

后端 未结 5 579
深忆病人
深忆病人 2021-01-05 03:26

I have to make a function that takes an empty list as first argument and n as secound argument, so that:

L=[]
function(L,5)
print L
returns:
[1,2,3,4,5]
         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 03:45

    In

    def fillList(listToFill,n):
        listToFill=range(1,n+1)
    

    you change only the pointer of listToFill, if you don't return the new pointer; the new pointer isn't available out of the function and you have still the pointer of your empty list (in outer scope).

提交回复
热议问题