IAC 2024 - IA Fast Track to Search Focused AI Solutions
Collections in Python - Where Data Finds Its Perfect Home.pdf
1. Collections in Python - Where
Data Finds Its Perfect Home
Python is a powerful programming language with a vast standard
library. The collections module is one such library that is a part of the
standard library. It provides an alternative to Python’s built-in data
structures such as lists, dictionaries, and tuples.
The collections module contains various container data types such as
deque, OrderedDict, Counter, and defaultdict, etc.
In this guide, we will discuss the collections module and its different
data types in detail.
The collections module in Python provides alternatives to Python’s
built-in data structures. These alternatives are highly optimized for
2. different use cases and provide additional functionality beyond what is
available in the built-in data structures.
In this guide, we will cover the following Python collection data types:
1. Counter
2. OrderedDict
3. defaultdict
4. deque
5. namedtuple
3. 6. ChainMap
Counter:
The Counter class is a subclass of the dictionary class. It is used to
count the occurrences of elements in an iterable. The Counter object
takes an iterable as an argument and returns a dictionary where the
keys are the elements in the iterable, and the values are the count of
occurrences of those elements.
Here’s an example:
from collections import Counter
my_list = [1, 2, 2, 3, 3, 3]
my_counter = Counter(my_list)
4. print(my_counter)
Output:
Counter({3: 3, 2: 2, 1: 1})
OrderedDict:
The OrderedDict class is a subclass of the dictionary class. It is used to
create dictionaries that remember the order in which the keys were
inserted. The order of the keys is maintained by a doubly-linked list.
Here’s an example:
from collections import OrderedDict
my_dict = OrderedDict()
my_dict['a'] = 1
my_dict['b'] = 2
5. my_dict['c'] = 3
print(my_dict)
Output:
OrderedDict([(‘a’, 1), (‘b’, 2), (‘c’, 3)])
defaultdict:
The defaultdict class is a subclass of the dictionary class. It is used to
create dictionaries with default values for keys that do not exist. The
default value is specified using a callable object that takes no
arguments and returns the default value.
Here’s an example:
from collections import defaultdict
6. my_dict = defaultdict(int)
my_dict['a'] = 1
my_dict['b'] = 2
print(my_dict['a'])
print(my_dict['c'])
Output:
1
0
deque:
The deque class is a subclass of the list class. It is used to create
double-ended queues. Deques are thread-safe and can be used for
multi-threaded applications.
8. The namedtuple class is used to create tuple subclasses with named
fields. Namedtuples are immutable and can be accessed using dot
notation.
Here’s an example:
from collections import namedtuple
Person = namedtuple('Person', ['name', 'age', 'gender'])
person = Person('John', 30, 'Male')
print(person.name)
print(person.age)
print(person.gender)
9. Output:
John
30
Male
ChainMap:
The ChainMap class is used to combine multiple dictionaries into a
single dictionary. It isused to search for a key in multiple dictionaries
in the order in which they are provided.
Here’s an example:
from collections import ChainMap
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
10. combined_dict = ChainMap(dict1, dict2)
print(combined_dict['a'])
print(combined_dict['d'])
Output:
1
4
When to use Python Collections:
Python collections can be used in various scenarios where Python’s
built-in data structures such as lists, dictionaries, and tuples are not
efficient or optimized for the use case.
The collections module provides alternative data structures that are
highly optimized for specific use cases and provide additional
functionality beyond what is available in the built-in data structures.
11. Here are some scenarios where Python collections can be used:
1. Counting elements in an iterable:
The Counter class provided by the collections module is used to count
the occurrences of elements in an iterable. This can be used in various
scenarios, such as counting the frequency of words in a document,
counting the occurrence of elements in a list, etc.
2. Maintaining order in a dictionary:
The OrderedDict class provided by the collections module is used to
create dictionaries that remember the order in which the keys were
inserted. This can be used in scenarios where the order of the keys in
the dictionary matters, such as creating a configuration file.
3. Default values for missing keys in a dictionary:
The defaultdict class provided by the collections module is used to
create dictionaries with default values for keys that do not exist. This
can be used in scenarios where you need to set a default value for a key
that does not exist in a dictionary.
12. 4. Efficient deque operations:
The deque class provided by the collections module is used to create
double-ended queues. Deques are highly optimized for operations
such as adding and removing elements from the beginning and end of
the queue.
5. Named tuple:
The namedtuple class provided by the collections module is used to
create tuple subclasses with named fields. Namedtuples are
immutable and can be accessed using dot notation. This can be used in
scenarios where you need to define a simple class with immutable
attributes.
6. Combining multiple dictionaries:
The ChainMap class provided by the collections module is used to
combine multiple dictionaries into a single dictionary. It is used to
search for a key in multiple dictionaries in the order in which they are
provided. This can be used in scenarios where you need to search for a
13. key in multiple dictionaries and return the value from the first
dictionary that contains the key.
In conclusion, Python collections can be used in various scenarios
where the built-in data structures are not efficient or optimized for the
use case.
The collections module provides optimized data structures that are
highly efficient and provide additional functionality beyond what is
available in the built-in data structures.
Conclusion:
In conclusion, the collections module in Python provides efficient and
optimized data structures that can be used for various use cases.
It provides alternatives to Python’s built-in data structures and
provides additional functionality beyond what is available in the
built-in data structures.
14. The data types provided by the collections module include Counter,
OrderedDict, defaultdict, deque, namedtuple, and ChainMap. These
data types can be used to create efficient and optimized data
structures for various use cases.