so whats the difference between:
x = [1,1,1], [1,1,1], [1,1,1], [1,1,1] and
x = [[1,1,1], [1,1,1], [1,1,1], [1,1,1]] do the square brackets around the second option do anything?
so whats the difference between:
x = [1,1,1], [1,1,1], [1,1,1], [1,1,1] and
x = [[1,1,1], [1,1,1], [1,1,1], [1,1,1]] do the square brackets around the second option do anything?
The first one creates a tuple of lists , while the second one creates a list of lists.
tuples are immutable, whereas lists are mutable.
Example for 1st one -
>>> x = [1,1,1], [1,1,1], [1,1,1], [1,1,1] >>> x ([1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]) >>> x[0] = 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment Example for 2nd one -
>>> x = [[1,1,1], [1,1,1], [1,1,1], [1,1,1]] >>> x [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] >>> x[0] = 1 >>> x [1, [1, 1, 1], [1, 1, 1], [1, 1, 1]] Without the brackets it is a tuple:
>>> x = [1,1,1], [1,1,1], [1,1,1], [1,1,1] >>> x ([1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]) >>> type(x) <type 'tuple'> You can't modify the items in a tuple.
With [] is it a list:
>>> x = [[1,1,1], [1,1,1], [1,1,1], [1,1,1]] >>> x [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] >>> type(x) <type 'list'>