No matter what some folks will argue or say, understanding key differences between python collection data types is essential to pave your way from much headaches. I got this important explanation from this
book . In fact in python or even other similar programming languages we have three types of Collection data types : Sequences, sets and mappings.
Sequences: A sequence type is one that supports the membership operator (in), the sizefunction (len()), slices ([]), and is iterable. Python provides five built-in sequence types: bytearray, bytes, list, str, and tuple:
- Tuples: A tuple is an ordered
sequence of zero or more object references. It supports the same slicing
and striding syntax as strings.Like strings, tuples are immutable, so
we cannot replace or delete any of their items. If we want to be able to
modify an ordered sequence, we simply use a list instead of a tuple; or
if we already have a tuple but want to modify it, we can convert it to a
list using the list() conversion function and then apply the changes to
the resultant list. e.g: myTyple= 'soulemane', 'cse', 2014, -1, 'y'
- Lists:
A list is an ordered sequence of zero or more object references. Lists
support the same slicing and striding syntax as strings and tuples. This
makes it easy to extract items from a list. Unlike strings and tuples,
lists are mutable, so we can replace and delete any of their items. It
is also possible to insert, replace,and delete slices of lists.
e.g: myList= ['soulemane', 'cse', 2014, -1, 'y']
Sets: A set type is a collection data type that supports the membership operator (in), the size function (len()), and is iterable.Python provides two built-in set types: the mutable set type and the immutable frozenset. When iterated, set types provide their items in an arbitrary order.
All the built-in immutable data types, such as float, frozenset, int, str, and tuple, are hashable and
can be added to sets. The built-in mutable data types, such as dict, list, and set, are not hashable since
their hash value changes depending on the items they contain, so they cannot be added to sets.
- Sets: A set is an unordered collection of zero or more object references that refer tohashable objects. Sets are mutable, so we can easily add or remove items, but since they are unordered they have no notion of index position and so cannotbe sliced or strided.
e.g: myList= {'soulemane', ('cse', 2014), -1, 'y'}
- Frozen Sets: A frozen set is a set that, once created, cannot be changed. We can of course rebind the variable that refers to a frozen set to refer to something else, though. Frozen sets can only be created using the frozenset data type called as a function. Since frozen sets are immutable, they support only those methods and operators that produce a result without affecting the frozen set or sets to which they are applied.
Mappings: A mapping type is one that supports the membership operator (in) and the size function (len()), and is iterable. Mappings are collections of key–value items and provide methods for accessing items and their keys and values. When iterated, unordered mapping types provide their items in an arbitrary order.
Only hashable objects may be used as dictionary keys, so immutable data types such as float, frozenset, int, str, and tuple can be used as dictionary keys, but mutable types such as dict, list, and set cannot. On the other hand, each key’s associated value can be an object reference referring to an object of any type, including numbers, strings, lists, sets, dictionaries, functions, and so on.
- Dictionaries: A dict is an unordered collection of zero or more key–value pairs whose keys are object references that refer to hashable objects,and whose values are object references referring to objects of any type. Dictionaries are mutable, so we can easily add or remove items, but since they are unordered they have no notion of index position and so cannot be sliced or strided.
e.g: myDict = dict({"Nb": 2014, "soulemane": "moumie", "salary": 10})
or
myDict = {"Nb": 2014, "soulemane": "moumie", "salary": 10}
One important remarks is to notice maps or dictionary are defined with braces {} with each key-value pair element separated with a colon , sets are defined with braces {}, lists are defined with brackets [] , and tuples are directly and openly defined.