Python: what is the difference between (1,2,3) and [1,2,3], and when should I use each?

后端 未结 17 2188
旧巷少年郎
旧巷少年郎 2020-12-29 01:24

In many places, (1,2,3) (a tuple) and [1,2,3] (a list) can be used interchangeably.

When should I use one or the other, and why?

17条回答
  •  长情又很酷
    2020-12-29 01:53

    Whenever I need to pass in a collection of items to a function, if I want the function to not change the values passed in - I use tuples.

    Else if I want to have the function to alter the values, I use list.

    Always if you are using external libraries and need to pass in a list of values to a function and are unsure about the integrity of the data, use a tuple.

提交回复
热议问题