python – Whats the difference between () vs [] vs {}?
python – Whats the difference between () vs [] vs {}?
() – tuple
A tuple is a sequence of items that cant 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).
- () 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:
python – Whats the difference between () vs [] vs {}?
() - tuple
[] - list
{} - dictionary
All Python tutorials should cover this. Here is a good place to start.
Related posts
- Python – TypeError: int object is not iterable
- python – How do I sort a dictionary by value?
- Does Python have a string contains substring method?
- python – Convert bytes to a string
- What is the Python equivalent for a case/switch statement?
- python – How do I list all files of a directory?
- python – How to copy files?