Open In App

Python DSA Libraries

Last Updated : 11 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Package or Library to Implement Array in Python

Array in Python can be created by importing an array module. array(data_typevalue_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

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

  • array.itemsize()
  • array.buffer_info()
  • array.count(x)
  • array.extend(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

Python3




import array
 
# Create an array of integers
int_array = array.array('i', [1, 2, 3, 4, 5])
 
# Access elements
print(int_array[0])


Output

1



Package or Library to Implement Linked list in Python

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 ‘collections.deque 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.
  • 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

Python3




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 ‘queue.Queue library’ in Python is used to implement Queue in Python.

What is ‘Queue module’ in Python?

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

  • Queue.qsize(): Return the approximate size of the queue.
  • Queue.empty(): Return True if the queue is empty, False otherwise.
  • Queue.full(): Return True if the queue is full, False otherwise.
  • Queue.put(): Put item into the queue.
  • Queue.get(): Remove and return an item from the queue.

Example to use Queue Library in Python

Python3




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

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 ‘collections.Counter 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. Counter.elements(): 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. Counter.subtract([iterable-or-mapping]): Elements are subtracted from an iterable or from another mapping (or counter).
  4. Counter.total(): Compute the sum of the counts.
  5. Counter.update([iterable-or-mapping]): Elements are counted from an iterable or added-in from another mapping (or counter).
  6. Counter.fromkeys(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

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 collections.ChainMap, collections.defaultdict, and collections.OrderedDict 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}

# 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.

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

Python3




# importing "heapq" to implement heap queue
import heapq
 
# initializing list
li = [5, 7, 9, 1, 3]
 
# using heapify to convert list into heap
heapq.heapify(li)
 
# printing created heap
print ("The created heap is : ",(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.
  • 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

Python3




from treelib import Node, Tree
 
# Create a new binary tree
tree = Tree()
 
# Add nodes to the tree
tree.create_node("Root", "root"# Create the root node
# Create a left child node
tree.create_node("Left Child", "left", parent="root"
# Create a right child node
tree.create_node("Right Child", "right", parent="root"
 
# Add more nodes to the left child
 # Create a left grandchild node
tree.create_node("Left Grandchild", "left_grand", parent="left")
# Create a right grandchild node
tree.create_node("Right Grandchild", "right_grand", parent="left"
 
# Print the tree structure
print("Tree structure:")
tree.show()
 
# Traverse the tree (pre-order traversal)
print("\nPre-order traversal:")
for node in tree.all_nodes():
    print(node.tag)
 
# Visualize the tree
tree.show(line_type="ascii-em")
 
# Visualize the tree using Graphviz (requires Graphviz installed)
# tree.show()


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

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()
  • bisect.insort()

Example to use Bisect Library in Python

Python3




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)
 
print("Sorted list after insertion:", sorted_list)
print("New element inserted at index:", 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.
  • 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

Python3




from intervaltree import IntervalTree, Interval
 
# Create an interval tree
tree = IntervalTree()
 
# Add intervals to the tree
tree.add(Interval(1, 5))
tree.add(Interval(3, 8))
tree.add(Interval(6, 10))
tree.add(Interval(12, 15))
 
# Query intervals that overlap with a given range
query_range = (4, 7)
result_intervals = tree.search(*query_range)
 
print("Intervals that overlap with the query range:", result_intervals)
 
# Iterate over the result intervals and print their start and end points
print("Start and end points of the overlapping intervals:")
for interval in result_intervals:
    print("Start:", interval.begin, "End:", interval.end)


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
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

Python3




from trie import Trie
 
# Create a new Trie object
trie = Trie()
 
# Insert some words into the trie
trie.insert("apple")
trie.insert("banana")
trie.insert("app")
trie.insert("bat")
trie.insert("ball")
 
# Search for words in the trie
print("Search Results:")
print("Does 'apple' exist?", trie.search("apple"))  # Output: True
print("Does 'app' exist?", trie.search("app"))      # Output: True
print("Does 'orange' exist?", trie.search("orange"))  # Output: False
 
# Check if any word starts with a given prefix
print("\nStartsWith Results:")
print("Does any word start with 'ap'?", trie.startswith("ap"))  # Output: True
print("Does any word start with 'ora'?", trie.startswith("ora"))  # Output: False
 
# Get autocomplete suggestions for a given prefix
print("\nAutocomplete Suggestions for 'ba':", trie.autocomplete("ba"))  # Output: ['ball', 'banana', 'bat']
 
# Delete a word from the trie
trie.delete("apple")
print("\nAfter deleting 'apple':", trie.words())  # Output: ['app', 'ball', 'banana', 'bat']
 
# Count the total number of words in the trie
print("\nTotal Number of Words:", trie.count_words())  # Output: 4
 
# Count the number of words with a given prefix
print("Number of words with prefix 'ba':", trie.count_prefixes("ba"))  # Output: 3


Related Article: Python Data Structures and Algorithms



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads