What's the difference between () vs [] vs {} in Python?
They're collections? How can I tell when to use which?

- 9,359
- 10
- 36
- 49
-
2Which tutorial are you reading? This is usually covered pretty thoroughly. Please identify the resources you're currently using to learn Python. – S.Lott Dec 10 '10 at 14:24
-
I've read several but I'm very forgetful, at the moment I'm just doing some https://github.com/gregmalcolm/python_koans - learning by doing. – Zolomon Dec 10 '10 at 16:45
-
Do more :-) From a python REPL (such as [trypython.org](http://trypython.org)) evaluating `type([])` will result in "
". Happy coding. – Dec 10 '10 at 20:30
5 Answers
() - tuple
A tuple is a sequence of items that can't be changed (immutable).
[] - list
A list is a sequence of items that can be changed (mutable).
{} - dictionary or set
A dictionary is a list of key-value pairs, with unique keys (mutable). From Python 2.7/3.1, {}
can also represent a set of unique values (mutable).

- 951,095
- 183
- 1,149
- 1,285
-
4N.B. From 2.7/3.1, {} is either a dictionary (`{"a":"1","b":"2"}`) or a set (`{"a","b","c"}`). A set is an unordered collection of unique values, which can be changed (although each item in it can't). – Thomas K Dec 10 '10 at 11:26
-
In general: a tuple is *heterogeneous* while a list is *homogeneous*. Of course the distinction/enforcement is blurred with dynamic typing. See the link in Mark Byers's answer. – Dec 10 '10 at 20:33
- () is a tuple: An immutable collection of values, usually (but not necessarily) of different types.
- [] is a list: A mutable collection of values, usually (but not necessarily) of the same type.
- {} is a dict: Use a dictionary for key value pairs.
For the difference between lists and tuples see here. See also:

- 1
- 1

- 811,555
- 193
- 1,581
- 1,452
() - tuple
[] - list
{} - dictionary
All Python tutorials should cover this. Here is a good place to start.

- 126,773
- 69
- 172
- 181
In addition to the tuple, list and dict given by the other answers, {}
also denotes a set literal in python 2.7 and python 3.1. (This makes sense because set elements act like the keys of a dict).

- 26,554
- 4
- 50
- 59
To complete the other answers about {}
:
If you see a = {"key1": 1, "key2": 2, "key3": 3}
(keys and values), then it's a dict
.
If you see a = {1, 2, 3}
(values only), then it's a set
.
If you see a = {}
(empty), then it's a dict
. An empty set
is created with a = set()
.
Quoting the official doc:
5.4. Sets
Python also includes a data type for sets. A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
Curly braces or the set() function can be used to create sets. Note: to create an empty set you have to use set(), not {}; the latter creates an empty dictionary, a data structure that we discuss in the next section.

- 21,083
- 14
- 84
- 130