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

后端 未结 17 2118
旧巷少年郎
旧巷少年郎 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:33

    • A tuple might represent a key in dictionary, because it's immutable.
    • Use lists if you have a collection of data that doesn't need random access.

提交回复
热议问题