-1

What is the defference between foo = (1,2,3) and foo = [1,2,3] in python Can any body explain me the difference between them more clearly.

cweiske
  • 28,704
  • 13
  • 124
  • 186
Ranjitha
  • 83
  • 6

2 Answers2

1

The first is a tuple which is an immutable type.

>>> foo = (1,2,3)
>>> foo[0] = 42
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'tuple' object does not support item assignment

The second is a list, which is mutable.

>>> foo = [1,2,3]
>>> foo[0] = 42
>>> foo
[42, 2, 3]

There are other very important differences between lists and tuples. Please see this question and its answers:

Community
  • 1
  • 1
Mark Byers
  • 767,688
  • 176
  • 1,542
  • 1,434
0

foo = (1,2,3) gives you a tuple; foo = [1,2,3] gives you a list. maybe start here?

Alexander Corwin
  • 1,077
  • 6
  • 10