Open In App

llist module in Python

Last Updated : 25 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Up until a long time Python had no way of executing linked list data structure. It does support list but there were many problems encountered when using them as a concept of the linked list like list are rigid and are not connected by pointers hence take a defined memory space that may even be wasted if the list is not fully filled. In fact, to overcome the problems possessed by list Python’s dequeue data structure could also be employed to work like a linked list. But all of this has been covered because of llist.

llist module in Python

llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster than dequeue and even the standard list for that matter.

Installation

To make use of benefits provided by llist it has to be installed like any other python extension or module, using pip. The following command will do the job.

pip install llist

If not for this, it can be manually downloaded from http://pypi.python.org/pypi , then unpack resources and compile them with “python setup.py install”. Once the module is successfully installed you only need to import this module whenever needed.

Methods provided

Currently, llist provides the following two types of linked lists:

  1. singly linked list(sllist) : a linked list in which the nodes point to the node next to it.
  2. doubly linked list(dllist) : a linked list in which the nodes point to the node after it as well as to the one before it.

This module contains following objects:

  • dllist : object to implement doubly linked list
  • dllistnode : returns a new doubly linked list node, initialized optionally
  • dllistiterator: an iterator object for dllist
  • sllist : object to implement singly linked list
  • sllistnode : a singly linked list node, initialized optionally
  • sllistiterator : an iterator object for sllist

The following examples will help you understand better. They give basic ideas about the execution of the two types of list supported by llist: Example 1: sllist 

Python3




# importing packages
import llist
from llist import sllist, sllistnode
 
# creating a linked list
lst = sllist(['first', 'second', 'third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
lst.insertafter('fifth', node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# popping a value
# i.e. removing the last entry of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()


Output:

sllist([first, second, third])
sllistnode(first)
sllistnode(third)
3

sllist([first, second, third, fifth, fourth])
sllistnode(first)
sllistnode(fourth)
5

sllist([first, second, third, fifth])
sllistnode(first)
sllistnode(fifth)
4

sllist([first, third, fifth])
sllistnode(first)
sllistnode(fifth)
3

Example 2: dllist 

Python3




# importing packages
import llist
from llist import dllist, dllistnode
 
# creating a linked list
lst = dllist(['first', 'second', 'third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
lst.extendleft(['fifth', 'sixth'])
new_node = dllistnode('seventh')
ref_node = lst.nodeat(2)
lst.insertnode(new_node, ref_node)
 
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# popping a value
# i.e. removing the last entry of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()


Output:

dllist([first, second, third])
dllistnode(first)
dllistnode(third)
3

dllist([sixth, fifth, seventh, first, second, third, fourth])
dllistnode(sixth)
dllistnode(fourth)
7

dllist([sixth, fifth, seventh, first, second, third])
dllistnode(sixth)
dllistnode(third)
6

dllist([sixth, seventh, first, second, third])
dllistnode(sixth)
dllistnode(third)
5


Similar Reads

dllist class of llist module in Python
llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster that dequeue and even the standard list for that matter. Doubly Linked List It is a type of linked list in each node stores data as well as two addresses (address of nodes succeeding and preceding it). A much simpler definition would b
4 min read
sllist class of llist module in Python
llist is an extension module of CPython that provides a basic linked list structure. They are significantly faster than dequeue and even the standard list for that matter. Singly Linked List It is a simple unidirectional data structure hence it can be traversed only form head to the last node. Each node of a singly linked list contains data and the
4 min read
Os Module Vs. Sys Module In Python
Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you will be able to easily decide which module suits
5 min read
MySQL-Connector-Python module in Python
MySQL is a Relational Database Management System (RDBMS) whereas the structured Query Language (SQL) is the language used for handling the RDBMS using commands i.e Creating, Inserting, Updating and Deleting the data from the databases. SQL commands are case insensitive i.e CREATE and create signify the same command. In this article, we will be disc
2 min read
twitter-text-python (ttp) module - Python
twitter-text-python is a Tweet parser and formatter for Python. Amongst many things, the tasks that can be performed by this module are : reply : The username of the handle to which the tweet is being replied to. users : All the usernames mentioned in the tweet. tags : All the hashtags mentioned in the tweet. urls : All the URLs mentioned in the tw
3 min read
Python calendar module : formatmonth() method
Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions. class calendar.TextCalendar(firstweekday=0) can be used to generate plain text
2 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick out few handful information to make small changes b
3 min read
median() function in Python statistics module
Python is a very popular language when it comes to data analysis and statistics. Luckily, Python3 provide statistics module, which comes with very useful functions like mean(), median(), mode() etc.median() function in the statistics module can be used to calculate median value from an unsorted data-list. The biggest advantage of using median() fun
4 min read
Stack and Queue in Python using queue Module
A simple python List can act as queue and stack as well. Queue mechanism is used widely and for many purposes in daily life. A queue follows FIFO rule(First In First Out) and is used in programming for sorting and for many more things. Python provides Class queue as a module which has to be generally created in languages such as C/C++ and Java. 1.
3 min read
PyMsgBox module in Python
PyMsgBox is simple, cross-platform, purely implemented in Python for message boxes as JavaScript has. It uses Python's built-in Tkinter module for its GUI. Installation This module does not built-in Python. To install it type the below command in the terminal. pip install PyMsgBox There are four functions in PyMsgBox, which follow JavaScript’s mess
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg