-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
  • 30,033
  • 14
  • 133
  • 194
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
  • 811,555
  • 193
  • 1,581
  • 1,452
0

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

Alexander Corwin
  • 1,097
  • 6
  • 11