What does the list() function do in Python?

前端 未结 6 1573
暖寄归人
暖寄归人 2020-12-19 11:14

I know that the list() constructor creates a new list but what exactly are its characteristics?

  1. What happens when you call list((1,2,3,4,

6条回答
  •  天命终不由人
    2020-12-19 12:02

    Your question is vague, but this is the output as follows, it doesn't "replace" the outer braces, it creates a data structure of a list, that can contain any value in a "listed" order (one after the other, after the other, and so on...) in a recursive way, you can add/remove elements to a specified index using append and pop. By the other hand, tuples are static and are not dynamically linked, they are more like an array of any type of element.

    WHEN:

    list((1,2,3,4,[5,6,7,8],9))
    

    RETURNS:

    [1, 2, 3, 4, [5, 6, 7, 8], 9]
    

    WHEN:

    list([[[2,3,4]]])
    

    RETURNS:

    [[[2, 3, 4]]]
    

    WHEN:

    list([[1,2,3],[4,5,6]])
    

    RETURNS:

    [[1, 2, 3], [4, 5, 6]]
    

提交回复
热议问题