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

后端 未结 17 2174
旧巷少年郎
旧巷少年郎 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条回答
  •  猫巷女王i
    2020-12-29 01:37

    In simple words, lists are mutable whereas tuples are not. Hence, if you want to modify the elements in your program i.e., adding, deleting or altering elements, go for a list. But, if you don't want tat to happen i.e., may be for setting sequence in for loop, etc. go for a tuple

提交回复
热议问题