What does the list() function do in Python?

前端 未结 6 1610
暖寄归人
暖寄归人 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:09

    You said: "From what I can tell, calling the constructor list removes the most outer braces (tuple or list) and replaces them with []. Is this true?"

    IMHO, this is not a good way to think about what list() does. True, square brackets [] are used to write a list literal, and are used when you tell a list to represent itself as a string, but ultimately, that's just notation. It's better to think of a Python list as a particular kind of container object with certain properties, eg it's ordered, indexable, iterable, mutable, etc.

    Thinking of the list() constructor in terms of it performing a transformation on the kind of brackets of a tuple that you pass it is a bit like saying adding 3 to 6 turns the 6 upside down to make 9. It's true that a '9' glyph looks like a '6' glyph turned upside down, but that's got nothing to do with what happens on the arithmetic level, and it's not even true of all fonts.

提交回复
热议问题