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

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

    (1,2,3) and [1,2,3] can be used interchangeably in rare conditions.

    So (1,2,3) is a tuple and is immutable. Any changes you wish to make need to overwrite the object.

    [1,2,3] is a list and elements can be appended and removed.

    List has more features than a tuple.

提交回复
热议问题