Take the content of a list and append it to another list

后端 未结 7 1757
死守一世寂寞
死守一世寂寞 2020-12-07 08:55

I am trying to understand if it makes sense to take the content of a list and append it to another list.

I have the first list created through a loop function, that

7条回答
  •  忘掉有多难
    2020-12-07 09:24

    If we have list like below:

    list  = [2,2,3,4]
    

    two ways to copy it into another list.

    1.

    x = [list]  # x =[] x.append(list) same 
    print("length is {}".format(len(x)))
    for i in x:
        print(i)
    
    length is 1
    [2, 2, 3, 4]
    

    2.

    x = [l for l in list]
    print("length is {}".format(len(x)))
    for i in x:
        print(i)
    
    length is 4
    2
    2
    3
    4
    

提交回复
热议问题