212
>>> x=[1,2]
>>> x[1]
2
>>> x=(1,2)
>>> x[1]
2

Are they both valid? Is one preferred for some reason?

ImtiazeA
  • 1,272
  • 14
  • 19
qazwsx
  • 25,536
  • 30
  • 72
  • 106
  • 3
    Just FYI: there's a more profound difference between `(i for i in ...)` and `[i for i in ...]`. – Rik Poggi Jan 17 '12 at 21:28
  • 4
    @RikPoggi What is the profound difference? Could you please elaborate? – qazwsx Jan 17 '12 at 21:49
  • 15
    The first one is a **generator expression** and the second one is a **list comprehension**. You can found some informations here: [Official Tutorial on List Comprehension](http://docs.python.org/py3k/tutorial/datastructures.html#list-comprehensions), [PEP 289](http://www.python.org/dev/peps/pep-0289/). And here in some OS questions: [Generator Expressions vs. List Comprehension](http://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehension), [generator-comprehension](http://stackoverflow.com/questions/364802/generator-comprehension). – Rik Poggi Jan 17 '12 at 22:08

7 Answers7

325

Square brackets are lists while parentheses are tuples.

A list is mutable, meaning you can change its contents:

>>> x = [1,2]
>>> x.append(3)
>>> x
[1, 2, 3]

while tuples are not:

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

The other main difference is that a tuple is hashable, meaning that you can use it as a key to a dictionary, among other things. For example:

>>> x = (1,2)
>>> y = [1,2]
>>> z = {}
>>> z[x] = 3
>>> z
{(1, 2): 3}
>>> z[y] = 4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

Note that, as many people have pointed out, you can add tuples together. For example:

>>> x = (1,2)
>>> x += (3,)
>>> x
(1, 2, 3)

However, this does not mean tuples are mutable. In the example above, a new tuple is constructed by adding together the two tuples as arguments. The original tuple is not modified. To demonstrate this, consider the following:

>>> x = (1,2)
>>> y = x
>>> x += (3,)
>>> x
(1, 2, 3)
>>> y
(1, 2)

Whereas, if you were to construct this same example with a list, y would also be updated:

>>> x = [1, 2]
>>> y = x
>>> x += [3]
>>> x
[1, 2, 3]
>>> y
[1, 2, 3]
Ondrej K.
  • 8,841
  • 11
  • 24
  • 39
jterrace
  • 64,866
  • 22
  • 157
  • 202
13

One interesting difference :

lst=[1]
print lst          // prints [1]
print type(lst)    // prints <type 'list'>

notATuple=(1)
print notATuple        // prints 1
print type(notATuple)  // prints <type 'int'>
                                         ^^ instead of tuple(expected)

A comma must be included in a tuple even if it contains only a single value. e.g. (1,) instead of (1).

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
4

They are not lists, they are a list and a tuple. You can read about tuples in the Python tutorial. While you can mutate lists, this is not possible with tuples.

In [1]: x = (1, 2)

In [2]: x[0] = 3
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/user/<ipython console> in <module>()

TypeError: 'tuple' object does not support item assignment
Gandaro
  • 3,427
  • 1
  • 17
  • 19
3

Another way brackets and parentheses differ is that square brackets can describe a list comprehension, e.g. [x for x in y]

Whereas the corresponding parenthetic syntax specifies a tuple generator: (x for x in y)

You can get a tuple comprehension using: tuple(x for x in y)

See: Why is there no tuple comprehension in Python?

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
xyzzy71
  • 41
  • 4
2

The first is a list, the second is a tuple. Lists are mutable, tuples are not.

Take a look at the Data Structures section of the tutorial, and the Sequence Types section of the documentation.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

Comma-separated items enclosed by ( and ) are tuples, those enclosed by [ and ] are lists.

Sufian Latif
  • 13,086
  • 3
  • 33
  • 70
  • 1
    There are no lists that are enclosed by "round" brackets, they are tuples! But you probably mean the right thing. `:P` – Gandaro Jan 17 '12 at 19:07
  • 1
    _Lists enclosed by ( and ) are tuples_ .. I'm confused, are they lists or tuples? – juliomalegria Jan 17 '12 at 19:33
  • 1
    @julio.alegria I think what @FlopCoder meant to write was "**Items** enclosed by `(` and `)` are `tuple`s, those enclosed by `[` and `]` are `list`s." – funroll May 29 '12 at 11:08
0

( thanx Robert for clarification, below is only in case of using listcomprehensions : )

! another very important difference is that with round brackets we will have a generator and so the memory consumption is much lower in comparison to list with square brackets esspecially when you deal with big lists - generator will eat not only significantly less memory but also will take much less time 'cause you will not need to prebuilt objects in list

  • Are you sure om that? Can you [edit] your answer and explain more why there is a generator? All other answers say that `(...)` is a tuple vs `[...]` being a list. AFAIK a generator is a function, but the questions has constants. – Robert Sep 16 '21 at 20:52