COLLECTIONS IN PYTHON
Collections in python are basically container data types, namely lists, sets, tuples, dictionary. They have different characteristics based on the declaration and the usage.
- A list is declared in square brackets, it is mutable, stores duplicate values and elements can be accessed using indexes.
- A tuple is ordered and immutable in nature, although duplicate entries can be there inside a tuple.
- A set is unordered and declared in square brackets. It is not indexed and does not have duplicate entries as well.
- A dictionary has key value pairs and is mutable in nature. We use square brackets to declare a dictionary.
These are the python’s general purpose built-in container data types. But as we all know, python always has a little something extra to offer. It comes with a python module named collections which has specialized data structures.
Specialized Collection Data Structures
Collections module in python implements specialized data structures which provide alternative to python’s built-in container data types. Following are the specialized data structures in collections module.
- namedtuple( )
- deque
- Chainmap
- Counter
- OrderedDict
- defaultdict
- UserDict
- UserList
- UserString
namedtuple( )
It returns a tuple with a named entry, which means there will be a name assigned to each value in the tuple. It overcomes the problem of accessing the elements using the index values. With namedtuple( ) it becomes easier to access these values, since you do not have to remember the index values to get specific elements.
Syntax:
class collections.namedtuple(typename, field_names)deque
deque pronounced as ‘deck’ is an optimized list to perform insertion and deletion easily.is the optimized list for quicker append and pop operations from both sides of the container. It provides O(1) time complexity for append and pop operations as compared to list with O(n) time complexity.
Syntax:
class collections.deque(list)ChainMap
It is a dictionary like class which is able to make a single view of multiple mappings. It basically returns a list of several other dictionaries. Suppose you have two dictionaries with several key value pairs, in this case ChainMap will make a single list with both the dictionaries in it.
Get Bhavatharani’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax:
class collections.ChainMap(dict1, dict2)Counters
A counter is a sub-class of the dictionary. It is used to keep the count of the elements in an iterable in the form of an unordered dictionary where the key represents the element in the iterable and value represents the count of that element in the iterable.
Note: It is equivalent to bag or multiset of other languages.
Syntax:
class collections.Counter([iterable-or-mapping])OrderedDict
An OrderedDict is also a sub-class of dictionary but unlike dictionary, it remembers the order in which the keys were inserted.
Syntax:
class collections.OrderDict()DefaultDict
A DefaultDict is also a sub-class to dictionary. It is used to provide some default values for the key that does not exist and never raises a KeyError.
Syntax:
class collections.defaultdict(default_factory)default_factory is a function that provides the default value for the dictionary created. If this parameter is absent then the KeyError is raised.
Initializing DefaultDict Objects
DefaultDict objects can be initialized using DefaultDict() method by passing the data type as an argument.
UserDict
UserDict is a dictionary-like container that acts as a wrapper around the dictionary objects. This container is used when someone wants to create their own dictionary with some modified or new functionality.
Syntax:
class collections.UserDict([initiaa])UserList
UserList is a list like container that acts as a wrapper around the list objects. This is useful when someone wants to create their own list with some modified or additional functionality.
Syntax:
class collections.UserList([list])UserString
UserString is a string like container and just like UserDict and UserList it acts as a wrapper around string objects. It is used when someone wants to create their own strings with some modified or additional functionality.
Syntax:
class collections.UserString(seq)






