0

in Python3.2 I'm trying to do a list:

>> ls = 1, 2, 3
>> ls
(1, 2, 3)
>> ls.append(4)

And with this last command, I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'append'

Why is this? How can I solve it? Thanks for your time

Randy Morris
  • 39,631
  • 8
  • 69
  • 76
Telmo Vaz
  • 189
  • 1
  • 2
  • 11

1 Answers1

6

The syntax for a list is [1, 2, 3].

(1, 2, 3) is a tuple.

Lists are mutable, but tuples are immutable. That is, tuples can't be modified after they have been created (which is why you can't append to them).

This answer has great insight as to when you'd use one over the other.

Community
  • 1
  • 1
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116