0% found this document useful (0 votes)
1 views19 pages

Python DSA Libraries - GeeksforGeeks

The document discusses various Python libraries essential for implementing Data Structures and Algorithms (DSA), including arrays, linked lists, queues, hash maps, heaps, and trees. It provides examples and explanations of key libraries such as 'array', 'collections.deque', 'queue.Queue', 'collections.Counter', 'heapq', and 'treelib'. Each section outlines important methods and usage examples to facilitate understanding and application of these libraries in Python.

Uploaded by

mydearkathyayani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views19 pages

Python DSA Libraries - GeeksforGeeks

The document discusses various Python libraries essential for implementing Data Structures and Algorithms (DSA), including arrays, linked lists, queues, hash maps, heaps, and trees. It provides examples and explanations of key libraries such as 'array', 'collections.deque', 'queue.Queue', 'collections.Counter', 'heapq', and 'treelib'. Each section outlines important methods and usage examples to facilitate understanding and application of these libraries in Python.

Uploaded by

mydearkathyayani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

Search... Sign In

Python Tutorial Data Types Interview Questions Examples Quizzes DSA Python Data Scien

Python DSA Libraries


Last Updated : 23 Jul, 2025

Data Structures and Algorithms (DSA) serve as the backbone for


efficient problem-solving and software development. Python, known
for its simplicity and versatility, offers a plethora of libraries and
packages that facilitate the implementation of various DSA concepts.
In this article, we'll delve into some essential Python libraries for DSA,
covering arrays, linked lists, queues, hash maps, heaps, trees, and
specialized algorithms like Bisect, Interval Trees, and Trie Trees.

Table of Content
Array in Python
Linked list in Python
Queue in Python
Hash Map in Python
Heap in Python
Tree in Python
Bisect Algorithm in Python
Interval Tree in Python
Trie Tree in Python

Package or Library to Implement Array in Python


Array in Python can be created by importing an array
module. array(data_type, value_list) is used to create an array with
data type and value list specified in its arguments.

Package or Library to implement Array in Python

The 'array library' in Python is used to implement Arrays in Python

[Link] 1/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

What is an 'array module' in Python?

An array module in Python defines an object type that can compactly


represent an array of basic values: characters, integers, and floating-
point numbers.

Important Methods in Array library

[Link]()
array.buffer_info()
[Link](x)
[Link](iterable)

Syntax to use Array Library in Python:

# Importing array module


import array as <module variable>
# Creating an array in Python using Array Module
<array variable> = <module variable>.array('<data type of
elements>', <list of elements of specified type>)

Example to use Array Library in Python

import array

# Create an array of integers


int_array = [Link]('i', [1, 2, 3, 4, 5])

# Access elements
print(int_array[0])

Output

Package or Library to Implement Linked list in


Python
[Link] 2/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

Linked List consists of a sequence of elements called nodes, where


each node contains some data and a reference (or pointer) to the next
node in the sequence. The last node typically points to null to indicate
the end of the list.

Package or Library to implement Array in Python

The '[Link] library' in Python is used to implement Linked


list in Python.

What is 'deque module' in Python?

In Python, the collections module provides a versatile deque class,


which stands for "double-ended queue". Although it's not specifically
named as a "double linked list", it internally uses a doubly linked list
structure to provide efficient insertion and deletion operations at both
ends of the queue.

Important Methods in deque library

append(ele): Add ele to the right side of the deque


appendleft(ele): Add ele to the left side of the deque.
clear():Remove all elements from the deque leaving it with length
0.
copy(): Create a shallow copy of the deque.
count(ele): Count the number of deque elements equal to x.
extend(): Extend the right side of the deque by appending elements
from the iterable argument.
extendleft(): Extend the left side of the deque by appending
elements from iterable.
index(): Return the position of x in the deque. Returns the first
match.
insert(): Insert x into the deque at position i.
pop(): Remove and return an element from the right side of the
deque.

[Link] 3/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

popleft(): Remove and return an element from the left side of the
deque.
remove(): Remove the first occurrence of value.
reverse(): Reverse the elements of the deque.
rotate(): Rotate the deque n steps to the right.

Example to use Deque Library in Python

from collections import deque

# Creating a deque
my_queue = deque()

# Adding elements to the queue


my_queue.append(1) # Adds to the right end
my_queue.appendleft(2) # Adds to the left end

# Removing elements from the queue


element = my_queue.pop() # Removes and returns from the
right end
element = my_queue.popleft() # Removes and returns from the
left end

# Other methods available in deque include: extend,


extendleft, rotate, etc.

Package or Library to Implement Queue in Python


In a queue, elements are added (enqueue operation) to the rear (also
called the "tail") and removed (dequeue operation) from the front (also
called the "head"). This ensures that the oldest elements are
processed first, while newer elements are added to the end of the
queue.

Package or Library to Implement Hash Map in Python

The '[Link] library' in Python is used to implement Queue in


Python.

What is 'Queue module' in Python?


[Link] 4/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

In Python, the queue module provides various classes that implement


multi-producer, multi-consumer queues. These classes are designed
for use in multi-threaded programming and are especially useful for
communication between threads safely.

Important Methods in Counter library

[Link](): Return the approximate size of the queue.


[Link](): Return True if the queue is empty, False otherwise.
[Link](): Return True if the queue is full, False otherwise.
[Link](): Put item into the queue.
[Link](): Remove and return an item from the queue.

Example to use Queue Library in Python

from queue import Queue

# Creating a queue
my_queue = Queue()

# Adding elements to the queue


my_queue.put(1)
my_queue.put(2)
my_queue.put(3)

# Removing elements from the queue


# Removes and returns the first element added (FIFO)
element = my_queue.get()
print(element)

# Checking if the queue is empty


print(my_queue.empty())

# Getting the size of the queue


print(my_queue.qsize())

# Other methods available in Queue include: empty,


# qsize, full, task_done, join, etc.

Output

1
False
2

[Link] 5/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

Package or Library to Implement Hash Map in


Python
A hash map, also known as a hash table, is a data structure that stores
key-value pairs. It provides efficient insertion, deletion, and lookup
operations. Hash maps work by using a hash function to map keys to
indices in an array.

Package or Library to implement Hash Map in Python

The '[Link] library' in Python is used to implement Hash


Map in Python.

What is 'Counter Module' in Python?

A Counter in Python's collections module is a specialized dictionary


designed for counting hashable objects. It's particularly useful for
counting the occurrences of elements in a collection (e.g., a list or a
string). The Counter class provides methods for counting elements
efficiently and performing operations like addition, subtraction,
intersection, and union of counts.

Important Methods in Counter Library

Certainly! Here are some of the key methods provided by Counter


objects in Python's collections module, along with explanations for
each:

1. [Link](): Returns an iterator over elements repeating


each as many times as its count. The elements are returned in
arbitrary order.
2. Counter.most_common([n]): Returns a list of the n most common
elements and their counts from the most common to the least. If n
is omitted or None, returns all elements in the counter.
3. [Link]([iterable-or-mapping]): Elements are subtracted
from an iterable or from another mapping (or counter).
4. [Link](): Compute the sum of the counts.
[Link] 6/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

5. [Link]([iterable-or-mapping]): Elements are counted


from an iterable or added-in from another mapping (or counter).
6. [Link](iterable, value): Class method that creates a
new Counter object from an iterable and initializes each element's
count to the specified value.

Example to use Counter Library in Python

from collections import Counter

# Create a Counter object from a list


my_list = ['apple', 'banana', 'apple', 'orange', 'apple',
'banana']
my_counter = Counter(my_list)

print(my_counter)

Output

Counter({'apple': 3, 'banana': 2, 'orange': 1})

Efficient Libraries for Managing Dictionaries

Also there are [Link], [Link], and


[Link] Method inside the collection Library. Here,
The Counter itself doesn't inherently utilize it, but you might use them
together in certain scenarios, depending on your specific requirements.
What is 'ChainMap Library' in Python?
A ChainMap is a class in Python's collections module that provides the
ability to link multiple mappings together to create a single view. It
allows you to search multiple dictionaries as if they were one.

# Python program to demonstrate ChainMap


from collections import ChainMap

d1 = {'a': 1, 'b': 2}
d2 = {'c': 3, 'd': 4}
d3 = {'e': 5, 'f': 6}

[Link] 7/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

# Defining the chainmap


c = ChainMap(d1, d2, d3)

What is 'defaultdict Library' in Python?


The defaultdict is another handy class provided by the collections
module in Python. It's a subclass of the built-in dict class and provides
a convenient way to create dictionaries with default values for keys
that haven't been explicitly set.

from collections import defaultdict


# Define a defaultdict with default value 0
my_defaultdict = defaultdict(int)

What is 'OrderedDict Library' in Python?


The OrderedDict is a dictionary subclass provided by the collections
module in Python. It's similar to the built-in dict class but with one key
difference: it maintains the order of insertion of its keys.

from collections import OrderedDict


my_ordered_dict = OrderedDict()

Package or Library to Implement Heap in Python


A heap is a specialized tree-based data structure that satisfies the
heap property. Heaps are commonly implemented as binary trees,
specifically binary min-heaps or binary max-heaps.

Package or Library to Implement Hash Map in Python

The 'heapq library' in Python is used to implement Queue in Python.

What is 'heapq module' in Python?

The heapq module in Python provides a collection of heap-based


algorithms, specifically functions to implement heaps as regular lists
and perform heap operations efficiently. Despite being named "heapq",
it doesn't provide a separate heap data structure class. Instead, it
offers functions to manipulate regular Python lists as heaps.
[Link] 8/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

Important Methods in Counter library

The heapq module in Python provides functions rather than methods


for heap operations. Here are the main functions available in the heapq
module:

heapify(heap): This function transforms a list into a heap in linear


time.
heappush(heap, item): This function adds the item to the heap
while maintaining the heap property. It inserts the item at the
appropriate position within the heap.
heappop(heap): This function removes and returns the smallest
element from the heap.
heappushpop(heap, item): It pushes the item onto the heap and
then pops and returns the smallest element from the heap.
heapreplace(heap, item): This function first pops and returns the
smallest element from the heap before pushing the new item onto
the heap.
merge(iterables): This function merges multiple sorted inputs
(iterables) into a single sorted output (an iterator).
nlargest(n, iterable): This function returns the n largest elements
from the iterable, sorted in descending order.
nsmallest(n, iterable): This function returns the n smallest
elements from the iterable, sorted in ascending order.

Example to use Heapq Library in Python

# importing &quot;heapq&quot; to implement heap queue


import heapq

# initializing list
li = [5, 7, 9, 1, 3]

# using heapify to convert list into heap


[Link](li)

# printing created heap

[Link] 9/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

print (&quot;The created heap is : &quot;,(list(li)))

Output

The created heap is : [1, 3, 9, 7, 5]

Package to Implement Tree in Python


A tree is a hierarchical data structure consisting of nodes connected by
edges. It's a widely used data structure in computer science for
organizing data in a hierarchical manner.

Package or Library to Implement Tree in Python

The 'treelib library' in Python is used to implement Queue in Python.

What is 'treelib Module' in Python?

The treelib is a Python library that provides functionality for working


with tree structures. It allows you to create, manipulate, traverse, and
visualize tree data structures efficiently.

Important Methods in bisect library

create_node(tag, node_id=None, parent=None): Creates a new


node with the specified tag and optional data. Optionally specifies
the node identifier (node_id) and parent node (parent).
remove_node(node_id): Removes the node with the specified
identifier from the tree.
get_node(node_id): Retrieves the node with the specified identifier
from the tree.
update_node(node_id, tag=None, data=None): Updates the tag
and/or data of the node with the specified identifier.
contains(node_id): Checks if the tree contains a node with the
specified identifier.
parent(node_id): Returns the parent node identifier of the node
with the specified identifier.

[Link] 10/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

children(node_id): Returns a list of identifiers of the children nodes


of the node with the specified identifier.
depth(node_id): Calculates the depth of the node with the specified
identifier in the tree.
size(node_id=None): Calculates the size (number of nodes) of the
subtree rooted at the node with the specified identifier. If no
identifier is provided, calculates the size of the entire tree.
height(node_id=None): Calculates the height (maximum depth) of
the subtree rooted at the node with the specified identifier. If no
identifier is provided, calculates the height of the entire tree.
show(line_type="ascii"): Prints a textual representation of the tree

Example to use Bisect Library in Python

from treelib import Node, Tree

# Create a new binary tree


tree = Tree()

# Add nodes to the tree


tree.create_node(&quot;Root&quot;, &quot;root&quot;) #
Create the root node
# Create a left child node
tree.create_node(&quot;Left Child&quot;, &quot;left&quot;,
parent=&quot;root&quot;)
# Create a right child node
tree.create_node(&quot;Right Child&quot;, &quot;right&quot;,
parent=&quot;root&quot;)

# Add more nodes to the left child


# Create a left grandchild node
tree.create_node(&quot;Left Grandchild&quot;,
&quot;left_grand&quot;, parent=&quot;left&quot;)
# Create a right grandchild node
tree.create_node(&quot;Right Grandchild&quot;,
&quot;right_grand&quot;, parent=&quot;left&quot;)

# Print the tree structure


print(&quot;Tree structure:&quot;)
[Link]()

[Link] 11/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

# Traverse the tree (pre-order traversal)


print(&quot;\nPre-order traversal:&quot;)
for node in tree.all_nodes():
print([Link])

# Visualize the tree


[Link](line_type=&quot;ascii-em&quot;)

# Visualize the tree using Graphviz (requires Graphviz


installed)
# [Link]()

Output

Tree structure:
root
├── left
│ ├── left_grand
│ └── right_grand
└── right
Pre-order traversal:
root
left
left_grand
right_grand
right
root
|-- left
| |-- left_grand
| +-- right_grand
+-- right

Library to Implement Bisect Algorithm in Python


The bisect algorithm, also known as binary search, is a technique used
to efficiently find the position where an element should be inserted
into a sorted list to maintain the sorted order. It's named after the
bisect function provided by the bisect module in Python, which
implements this algorithm.

Package or Library to Implement Bisect Algorithm in Python

[Link] 12/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

The 'bisect library' in Python is used to implement Queue in Python.

What is 'bisect Module' in Python?

The bisect module in Python provides functions to efficiently insert


elements into sorted lists and find insertion points for new elements
while maintaining the sorted order. It's particularly useful when
dealing with sorted collections and needing to maintain their order
efficiently.

Important Methods in bisect library

As bisect module support additional methods, Please mention all the


methods in points with there explanation

bisect(list, num, beg, end): This function returns the position in the
sorted list.
bisect.bisect_left()
bisect.bisect_right()
bisect.insort_left()
bisect.insort_right()
[Link]()

Example to use Bisect Library in Python

import bisect

# Sorted list
sorted_list = [1, 3, 5, 7, 9]

# Element to insert
new_element = 6

# Find the insertion point using bisect_left


insertion_point = bisect.bisect_left(sorted_list, new_element)

# Insert the element into the sorted list


sorted_list.insert(insertion_point, new_element)

[Link] 13/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

print(&quot;Sorted list after insertion:&quot;, sorted_list)


print(&quot;New element inserted at index:&quot;,
insertion_point)

Output

Sorted list after insertion: [1, 3, 5, 6, 7, 9]


New element inserted at index: 3

Package to Implement Interval Tree in Python


An interval tree is a data structure used for efficiently storing and
querying intervals or ranges. It's a type of binary search tree
specifically designed to handle interval queries effectively.

Package or Library to Implement Bisect Algorithm in Python

The 'intervaltree library' in Python is used to implement Queue in


Python.

What is 'intervaltree Module' in Python?

The intervaltree library in Python is a data structure designed to


efficiently store and query intervals or ranges. It provides an
implementation of an interval tree, a type of binary search tree
optimized for interval queries.

Important Methods in intervaltree library

The intervaltree module in Python provides several methods for


efficiently working with interval trees.

add(interval): Adds an interval to the interval tree.


remove(interval): Removes an interval from the interval tree.
search(begin): Searches for intervals that overlap with the given
range defined by begin and end (inclusive).
overlap(begin): Alias for search(). Searches for intervals that
overlap with the given range defined by begin and end.

[Link] 14/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

at(begin): Searches for intervals that contain the specified point


begin. Returns a set of intervals that contain the point.
clear(): Clears all intervals from the interval tree, making it empty.
copy(): Creates a shallow copy of the interval tree, including all
intervals.
discard(interval): Removes an interval from the interval tree if it
exists, similar to the remove() method.
items(): Returns a generator that yields all intervals stored in the
interval tree.
size(): Returns the number of intervals stored in the interval tree.
empty(): Returns True if the interval tree is empty, False otherwise.

Example to use Intervaltree Library in Python

from intervaltree import IntervalTree, Interval

# Create an interval tree


tree = IntervalTree()

# Add intervals to the tree


[Link](Interval(1, 5))
[Link](Interval(3, 8))
[Link](Interval(6, 10))
[Link](Interval(12, 15))

# Query intervals that overlap with a given range


query_range = (4, 7)
result_intervals = [Link](*query_range)

print(&quot;Intervals that overlap with the query range:&quot;,


result_intervals)

# Iterate over the result intervals and print their start and end points
print(&quot;Start and end points of the overlapping intervals:&quot;)
for interval in result_intervals:
print(&quot;Start:&quot;, [Link], &quot;End:&quot;,
[Link])

Output

Intervals that overlap with the query range: {Interval(1, 5),


Interval(3, 8), Interval(6, 10)}
Start and end points of the overlapping intervals:
Start: 1 End: 5

[Link] 15/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

Start: 3 End: 8
Start: 6 End: 10

Package to Implement Trie Tree in Python


A Trie, also known as a prefix tree or digital tree, is a tree-like data
structure used to store a dynamic set of strings where the keys are
usually strings. Each node in a Trie represents a single character of a
string, and the path from the root to a particular node represents a
prefix of one or more strings.

Package or Library to Implement Trie in Python

The 'trie library' in Python is used to implement Queue in Python.

What is 'intervaltree Module' in Python?

The intervaltree library in Python is a data structure designed to


efficiently store and query intervals or ranges. It provides an
implementation of an interval tree, a type of binary search tree
optimized for interval queries.

Important Methods in intervaltree library

Trie(): Constructor method to create a new Trie object.


insert(str) -> None: Inserts a word into the trie.
search(str): Searches for a word in the trie. Returns True if the word
is found, otherwise False.
startswith(prefix: str): Checks if any word in the trie starts with the
given prefix. Returns True if a word starts with the prefix, otherwise
False.
delete(word: str): Deletes a word from the trie.
words(prefix: str = ''): Returns a list .of words in the trie that start
with the given prefix.

Example to use Trie Library in Python

from trie import Trie


[Link] 16/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks
from trie import Trie

# Create a new Trie object


trie = Trie()

# Insert some words into the trie


[Link](&quot;apple&quot;)
[Link](&quot;banana&quot;)
[Link](&quot;app&quot;)
[Link](&quot;bat&quot;)
[Link](&quot;ball&quot;)

# Search for words in the trie


print(&quot;Search Results:&quot;)
print(&quot;Does 'apple' exist?&quot;,
[Link](&quot;apple&quot;)) # Output: True
print(&quot;Does 'app' exist?&quot;,
[Link](&quot;app&quot;)) # Output: True
print(&quot;Does 'orange' exist?&quot;,
[Link](&quot;orange&quot;)) # Output: False

# Check if any word starts with a given prefix


print(&quot;\nStartsWith Results:&quot;)
print(&quot;Does any word start with 'ap'?&quot;,
[Link](&quot;ap&quot;)) # Output: True
print(&quot;Does any word start with 'ora'?&quot;,
[Link](&quot;ora&quot;)) # Output: False

# Get autocomplete suggestions for a given prefix


print(&quot;\nAutocomplete Suggestions for 'ba':&quot;,
[Link](&quot;ba&quot;)) # Output: ['ball',
'banana', 'bat']

# Delete a word from the trie


[Link](&quot;apple&quot;)
print(&quot;\nAfter deleting 'apple':&quot;, [Link]()) #
Output: ['app', 'ball', 'banana', 'bat']

# Count the total number of words in the trie


print(&quot;\nTotal Number of Words:&quot;,
trie.count_words()) # Output: 4

# Count the number of words with a given prefix

[Link] 17/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

print(&quot;Number of words with prefix 'ba':&quot;,


trie.count_prefixes(&quot;ba&quot;)) # Output: 3

Related Article: Python Data Structures and Algorithms

Comment S surajkr… Follow 2

Article Tags : Python Python-Library python-modules Python-DSA

Corporate & Communications Address:


A-143, 7th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar Pradesh
(201305)

Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305

Company Explore
About Us POTD
Legal Job-A-Thon
Privacy Policy Connect
Careers Blogs
Contact Us Nation Skill Up
Corporate Solution
Campus Training Program

Tutorials Courses
Programming Languages IBM Certification
DSA DSA and Placements
Web Technology Web Development
AI, ML & Data Science Data Science
DevOps Programming Languages
CS Core Subjects DevOps & Cloud

[Link] 18/19
10/10/25, 4:31 PM Python DSA Libraries - GeeksforGeeks

GATE GATE
School Subjects Trending Technologies
Software and Tools

Offline Centers Preparation Corner


Noida Interview Corner
Bengaluru Aptitude
Pune Puzzles
Hyderabad GfG 160
Patna System Design

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

[Link] 19/19

You might also like