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

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

    The list [1,2,3] is dynamic and flexible but that flexibility comes at a speed cost.

    The tuple (1,2,3) is fixed (immutable) and therefore faster.

提交回复
热议问题