list is a global name that may be overridden during runtime. list() calls that name. [] is always a list literal.
3. Brackets are used to enclose parenthetical materials within the parentheses while parentheses are used to enclose words, numbers, phrases, sentences, symbols, and other items in a sentence.
What is the difference between parentheses (), brackets [], and braces {} in a python code? Brackets are used to make lists Braces are used to make dictionary Parenthesis are used to make tuple But for indexing in all of those, only brackets are used.
What is a Python list? A list is an ordered and mutable Python container, being one of the most common data structures in Python. To create a list, the elements are placed inside square brackets ([]), separated by commas. As shown above, lists can contain elements of different types as well as duplicated elements.
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]
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)
.
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
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?
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.
Comma-separated items enclosed by (
and )
are tuple
s, those enclosed by [
and ]
are list
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With