Python Collections Module Tutorial - Python Tutorial | Logic Practice
Python Course / Advanced Features / Python Collections Module Tutorial

Python Collections Module Tutorial

BLUF: This lesson on Python Collections Module Tutorial provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: Python Collections Module Tutorial

Mastering Python Collections Module Tutorial is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

The Collections Module in Python stands apart from other standard modules. This module includes various container types designed for storing diverse object types.

The Python collections module is characterized as a container designed to hold groups of data, such as lists, dictionaries, sets, and tuples, among others. Its purpose is to enhance the capabilities of the standard collection containers. The collections module was first introduced in Python version 2.4.

Some common and widely used collection modules are as follows:

  • Counters
  • Default Dictionary
  • ChainMap
  • NamedTuple
  • Deque
  • UserDict
  • UserList
  • UserString
  • Counters

The Counters component is an extension of the Dictionary, utilized for tallying the number of elements in pairs. A dictionary functions as an unordered, mutable, and indexed collection in which each key is distinct and corresponds to a specific value. However, starting from Python 3.7, the order of insertion within the dictionary is preserved.

To utilize the ordereddict module, we must import it with the following command:

from collections import OrderedDict

Python Counter Syntax

It has the following syntax:

Example

class collections.OrderedDict()

Python Counter Example

Let's consider an example to illustrate the Counter collection module in Python.

Example

Example

#from collections import OrderedDict

from collections import OrderedDict

print("This is a Regular Dictionary:\n")

data = {}

data['m'] = 21

data['n'] = 22

data['o'] = 23

data['p'] = 24

for identifier, number in data.items():

    print(identifier, number)

print("\nThis is an Ordered Dictionary:\n")

ordered_data = OrderedDict()

ordered_data['e'] = 5

ordered_data['f'] = 6

ordered_data['g'] = 7

ordered_data['h'] = 8

for identifier, number in ordered_data.items():

    print(identifier, number)

Output:

Output

This is a Dictionary:

key1 21

key2 22

key3 23

key4 24

This is an Ordered Dict:

itemA 5

itemB 6

itemC 7

itemD 8

Example of deleting and reinserting a key

Let’s consider an example to illustrate how to remove and then reinsert a key utilizing the Python collection module.

Example

Example

from collections import OrderedDict

ordered_data = OrderedDict()

ordered_data['a'] = 150

ordered_data['b'] = 250

ordered_data['c'] = 350

ordered_data['d'] = 450

print('Prior to Removal')

for identifier, quantity in ordered_data.items():

    print(identifier, quantity)

# removing the entry

ordered_data.pop('a')

# Adding the same entry back

ordered_data['a'] = 150

print('\nAfter Adding Back')

for identifier, quantity in ordered_data.items():

    print(identifier, quantity)

Output:

Output

Before Removing

a 150

b 250

c 350

d 450

After Adding Back

b 250

c 350

d 450

a 150

DefaultDict

The DefaultDict is characterized by the provision of default values for keys that are absent, which allows it to avoid raising any errors.

Python DefaultDictSyntax

It has the following syntax:

Example

class data.defaultdict(factoryFunction)

The default dictionary can be created using the DefaultDict function, where data types are supplied as arguments.

Python DefaultDict Example

Let's consider an example to illustrate the DefaultDict collection module in Python.

Example

Example

from collections import defaultdict

# Creating a default dictionary with a default integer value of zero

frequencyCounter = defaultdict(int)

numbersList = [2, 5, 3, 4, 2, 3, 6, 7, 3]

# Tallying occurrences of each item in the list

for element in numbersList:

    frequencyCounter[element] += 1

print(frequencyCounter)

Output:

Output

defaultdict(<class 'int'>, {2: 3, 5: 2, 3: 4, 4: 2, 6: 2, 7: 2})

ChainMap

The ChainMap gathers and combines several dictionaries into one cohesive entity and subsequently produces a collection of dictionaries.

We can import the ChainMap by the following code:

Example

from collections import MultiDict

Python ChainMap Syntax

It has the following syntax:

Example

class collections.ChainMap(dictA, dictB)

Python ChainMap Example

To demonstrate the ChainMap collection module in Python, let us consider an example.

Example

Example

from collections import ChainMap

dataSet1 = {'x': 150, 'y': 250}

dataSet2 = {'z': 350, 'w': 450}

dataSet3 = {'u': 550, 'v': 650}

# combining the various dictionaries

combined = ChainMap(dataSet1, dataSet2, dataSet3)

# displaying the result

print(combined)

Output:

Output

ChainMap({'x': 150, 'y': 250}, {'z': 350, 'w': 450}, {'u': 550, 'v': 650})

The value can also be retrieved using the key name through the ChainMap.keys method, while the values method can similarly be employed for access.

Example

Example

# Importing ChainMap from the collections module

from collections import ChainMap

map1 = {'x': 150, 'y': 250}

map2 = {'z': 350, 'w': 450}

map3 = {'u': 550, 'v': 650}

# here we are creating the chain map

chain_map = ChainMap(map1, map2, map3)

# Accessing Values using key name

print(chain_map['x'])

# Accessing values using values() method

print(chain_map.values())

# Accessing keys using keys() method

print(chain_map.keys())

Output:

Output

ValuesView(ChainMap({'x': 150, 'y': 250}, {'z': 350, 'w': 450}, {'u': 550, 'v': 650}))

KeysView(ChainMap({'x': 150, 'y': 250}, {'z': 350, 'w': 450}, {'u': 550, 'v': 650}))

Adding a new dictionary

The new_child function allows us to insert a new dictionary at the start of the ChainMap.

Example

Example

#importing the collections module

import collections

# creating the dictionaries

first_dict = { 'x' : 321, 'y' : 654 }

second_dict = { 'y' : 987, 'z' : 222 }

third_dict = { 'g' : 1314 }

# initializing ChainMap

combined_chain = collections.ChainMap(first_dict, second_dict)

# printing chainMap

print ("The contents of the ChainMap are: ")

print (combined_chain)

# here we are using the new_child() to add new dictionary

updated_chain = combined_chain.new_child(third_dict)

# printing chainMap

print ("Showing new ChainMap : ")

print (updated_chain)

Output:

Output

The contents of the MultiMap are:

MultiMap({'x': 321, 'y': 654}, {'y': 987, 'z': 111})

Displaying updated MultiMap:

MultiMap({'m': 1313}, {'x': 321, 'y': 654}, {'y': 987, 'z': 111})

NamedTuple

A tuple is a collection of elements that is both ordered and immutable, indicating that its elements cannot be altered once it has been established. A NamedTuple resembles a tuple, with the sole distinction being that it contains named fields, enhancing the clarity, interpretation, and accessibility of the data.

The NamedTuple can be imported using the subsequent code:

from collections import namedtuple

Python NamedTuple Syntax

It has the following syntax:

Example

class collections.namedtuple(typeName, fields)

Python NamedTuple Example

Let's consider an example to illustrate the NamedTuple collection module in Python.

Example

Example

# Importing namedtuple from the collections module

from collections import namedtuple

# Creating namedtuple()

StaffMember = namedtuple('StaffMember', ['fullName', 'years', 'employeeID'])

# Creating an instance of a StaffMember

employee = StaffMember('David', '30', '123456')

# Access using index

print("The StaffMember years using index is: ", end="")

print(employee[1])

# Access using field name

print("The StaffMember employee ID using field name is: ", end="")

print(employee.employeeID)

print("The StaffMember full name is: ", end="")

print(employee.fullName)

Output:

Output

The Worker age using index is: 30

The Worker identification number using field title is: 350303

The Worker fullName is: Sarah

Deque

Deque is an abbreviation for Doubly Ended Queue. This data structure is part of collections and allows for the execution of append and pop operations at both ends of the structure.

The term "Deque" is frequently articulated like the word "Deck." The time complexity for the operations of pop and append is O(1), in contrast to the list, which has a time complexity of O(n).

Python Deque Syntax:

The Syntax for the deque data structure is:

Example

class collections.deque(items)

Python Deque Example

To illustrate the usage of the deque collection module in Python, let’s consider an example.

Example

Example

#from collections module, importing the deque data structure

from collections import deque

# Initializing deque

queue = deque(['userName','organization','employeeID'])

print(queue)

Output:

Output

deque(['userName', 'business', 'employeeID'])

Inserting Elements

By utilizing the deque data structure, we can add and insert elements at both ends of the deque. The append method is employed to insert elements on the right side, while the appendleft method is used for insertion on the left side.

Python Example for Inserting Elements

Consider an example to illustrate the process of adding an element to a deque utilizing the append method.

Example

Example

from collections import deque

# Creating a deque with starting elements

myDeque = deque([321, 654, 987])

# Now we add a number 2025 to the right side of the deque

myDeque.append(2025)

# Displaying the deque after adding to the right side

print("The deque after adding to the right is :")

print(myDeque)

# Next, we add a number to the left side of the deque

myDeque.appendleft(200)

# Displaying the deque after adding to the left side

print("The deque after adding to the left is :")

print(myDeque)

Output:

Output

The deque after adding to the right is :

deque([321, 654, 987, 2026])

The deque after adding to the left is :

deque([200, 321, 654, 987, 2026])

Removing Elements

Elements were incorporated on both the left and right sides utilizing the append method. In a similar fashion, elements can be eliminated from the deque through the pop method. The pop method is employed to remove an element from the right side of the deque, while popleft is used for removing an element from the left side.

Python Example to Remove Elements using Deque

Let’s consider an example to illustrate the process of eliminating elements from a deque utilizing the pop function in Python.

Example

Example

from collections import deque

# Setting up deque with starting values

myDeque = deque([2021, 2022, 2023, 2024, 2025])

# Now we remove an element from the right side of the deque

myDeque.pop()

# Displaying the deque after removing the element from the right side

print("The deque after removing from right is :")

print(myDeque)

# Next, we are removing the element from the left side of the deque

myDeque.popleft()

# Displaying the deque after removing the element from the left

print("The deque after removing from left is :")

print(myDeque)

Output:

Output

from collections import deque

# Initialize a deque with years
yearCollection = deque([2016, 2017, 2018, 2019])

# Remove an element from the right side
yearCollection.pop()
print("The deque after removing from right is :", yearCollection)

# Remove an element from the left side
yearCollection.popleft()
print("The deque after removing from left is :", yearCollection)

UserDict

UserDict functions as a wrapper around dictionary objects, serving as a container. It is utilized to construct a dictionary that includes additional features, functionalities, and more.

Python UserDict Syntax:

It has the following syntax:

Example

class collections.PersonDict([startingData])

Python UserDict Example

To illustrate the UserDict collection module in Python, let's consider an example.

Example

Example

from collections import UserDict

# We are defining a dictionary that restricts removal of items

class CustomDict(UserDict):

    # It prohibits the use of 'del' on the dictionary

    def __del__(self):

        raise RuntimeError("Removal not permitted")

    # It prevents the use of pop() on the dictionary

    def pop(self, key=None):

        raise RuntimeError("Removal not permitted")

    # It prevents the use of popitem() on the dictionary

    def popitem(self, key=None):

        raise RuntimeError("Removal not permitted")

# Create an instance of CustomDict

my_dict = CustomDict({'x': 150, 'y': 250, 'z': 350})

my_dict.pop(1)

Output:

Output

# Instantiate an object of MyDictionary
my_dict = MyDictionary({'x': 10, 'y': 20, 'z': 30})

# Attempt to remove an item
my_dict.remove(1)

UserList

Our examination revealed that UserDict functions as a wrapper for dictionary instances, while UserList serves as a container that encapsulates list objects. It is utilized for generating a string with additional functionalities, features, and so forth.

Python UserList Syntax:

It has the following syntax:

Example

class collections.ItemList([list])

Python UserList Example

Let’s consider an example to illustrate the UserList collection module found in Python.

Example

Example

#from collections import UserList

from collections import UserList

# Creating a list where object removal is restricted

class CustomList(UserList):

    # Disallows the use of remove() method on the list

    def remove(self, item=None):

        raise RuntimeError("Removal is not permitted")

    # Disallows the use of pop() method on the list

    def pop(self, index=None):

        raise RuntimeError("Removal is not permitted")

# Creating an instance of CustomList

myList = CustomList([150, 250, 350, 450])

print("Initial List")

# Appending 600 to the list

myList.append(600)

print("After Addition")

print(myList)

# Attempt to remove an item (will raise error)

myList.remove()

Output:

Output

Initial Collection

Following Addition

[150, 250, 350, 450, 550]

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

Cell In[6], line 24

     22 print(collection)

     23 # Try to eliminate an element (will trigger error)

---> 24 collection.remove()

Cell In[6], line 9

      8 def remove(self, item=None):

----> 9     raise RuntimeError("Removal not permitted")

RuntimeError: Removal not permitted

UserString

The UserList functions as a wrapper that encapsulates list objects. Similarly, the UserString serves as a wrapper around string objects. It is utilized for generating a string that includes additional functionalities, features, and so forth.

Python UserString Syntax

It has the following syntax:

Example

class collections.PersonString(sequence)

Python UserString Example

We will use an example to illustrate the UserString collection module in Python.

Example

Example

from collections import UserString

# Creating a Mutable Text

class text(UserString):

    # defining a method to add to text

    def add(self, s):

        self.data += s

    # defining a method to delete from text

    def delete(self, s):

        self.data = self.data.replace(s, "")

# inputting the text

myText = text("Sample")

print("Initial Text:", myText.data)

# adding the character to the text

myText.add("X")

print("Text After Adding:", myText.data)

# removing the character from the text

myText.delete("m")

print("Text after Deleting:", myText.data)

Output:

Output

original_text = "C# Guide"
appended_text = original_text + "GuideTechG"
modified_text = appended_text.replace("T", "G")

Conclusion

In this tutorial, we explored Python Collections. The Collections Module in Python stands apart from other built-in modules. This module comprises various types of containers designed for storing diverse object types. We examined several collection types, including Counters, Default Dictionary, ChainMap, NamedTuple, Deque, UserDict, UserList, and UserString.

Python Collections FAQs

1. What is the use of the collections in Python?

The Collections Module in Python distinguishes itself from other built-in modules. This module comprises various types of containers designed for the storage of diverse objects. Among the collections are Counter, DefaultDict, ChainMap, NamedTuple, and more.

2. Are collections built in Python, or do we have to install them separately?

Indeed, the collections module is integrated within Python, and we access it by using the command import collections. There is no need for a separate installation.

3. What is the use of defaultdict?

The DefaultDict includes predefined values for keys that are absent, which prevents it from generating any errors.

Example

from collections import defaultdict

# Create a dictionary with default values
default_dict = defaultdict(int)

# Adding elements to the dictionary
default_dict['Alice'] += 1
default_dict['Emma'] += 2
default_dict['David'] += 3

# Display the contents of the dictionary
for person, count in default_dict.items():
    print(person, count)

The default dictionary can be created by utilizing the DefaultDict function, providing data types as parameters.

4. How is ChainMap useful?

The ChainMap gathers and consolidates several dictionaries into one cohesive entity, subsequently producing a list of dictionaries.

We can import the ChainMap by the following code:

Example

from collections import MultiMap

5. What is the difference between NamedTuple and the Tuple?

A tuple is a collection of elements that is both ordered and immutable, indicating that its elements cannot be altered after it has been created.

A NamedTuple resembles a standard tuple, with the distinction that it is composed of named attributes, enhancing the clarity, understanding, and accessibility of the data.

The NamedTuple can be imported using the following code:

Example

from collections import struct

6. Why use deque instead of a list?

Deque is short for Doubly Ended Queue. This data structure is part of the collections framework and allows for appending and removing elements from both ends. The advantage of using a deque lies in its time complexity for pop and append operations, which is O(1), in contrast to O(n) for a list.

7. What is UserDict and when is it used?

UserDict acts as a wrapper around dictionary objects, functioning as a container. It is utilized for developing a dictionary that includes additional features, functions, and more.

8. Is NamedTuple mutable?

Similar to a Tuple, a NamedTuple is also characterized by its immutability, indicating that once it has been instantiated, its values remain unalterable.

9. What is Counter used for in Collections?

The Counters function is a component of the Dictionary, utilized to tally the number of elements within pairs. A dictionary serves as an unordered, mutable, and indexed collection in which each key is distinct and corresponds to its related value. However, starting from Python 3.7, the order of insertion in the dictionary is preserved.

10. Can we convert defaultdict to a normal dictionary?

Indeed, transforming a defaultdict into a standard dictionary is straightforward by utilizing dict(defaultdict_instance).

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience