Python Library for Linked List

Linked list is a simple data structure in programming, which obviously is used to store data and retrieve it accordingly. To make it easier to imagine, it is more like a dynamic array in which data elements are linked via pointers (i.e. the present record points to its next record and the next one points to the record that comes after it, this goes on until the end of the structure) rather than being tightly packed. 
There are two types of linked list: 

  1. Single-Linked List: In this, the nodes point to the node immediately after it
  2. Doubly Linked List: In this, the nodes not only references the node next to it but also the node before it.

Linked List in Python:

To start with Python, It does not have a linked list library built in to it like the classical programming languages. Python does have an inbuilt type list that works like a dynamic array but its operation shouldn’t be confused with a typical function of a linked list. This doesn’t mean one cannot implement linked list in Python, they can but it will not be straight up. The following methods can be used to implement linked list in Python since it does not have an inbuilt library for it:

Method 1: Using deque() package.
This is an inbuilt class in Python, obviously used for dequeue but can be implemented in such a way that it works like a linked list under certain conditions. 

Below is the implementation of linked list:

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

#importing module
import collections
  
#initialising a deque() of arbitary length
linked_lst = collections.deque()
  
#filling deque() with elements
linked_lst.append('first')
linked_lst.append('second')
linked_lst.append('third')
  
print("elements in the linked_list:")
print(linked_lst)
  
#adding element at an arbitary position
linked_lst.insert(1,'fourth')
  
print("elements in the linked_list:")
print(linked_lst)
  
#deleting the last element 
linked_lst.pop()
  
print("elements in the linked_list:")
print(linked_lst)
  
#removing a specific element
linked_lst.remove('fourth')
  
print("elements in the linked_list:")
print(linked_lst)

chevron_right


Output:

elements in the linked_list:
deque(['first', 'second', 'third'])
elements in the linked_list:
deque(['first', 'fourth', 'second', 'third'])
elements in the linked_list:
deque(['first', 'fourth', 'second'])
elements in the linked_list:
deque(['first', 'second'])

When to use deque() as a linked list ?

  • Inserting and deleting elements at front and back respectively is the only need. Inserting and removing elements from the middle becomes time-consuming.
  • In-place reversal since Python now allows elements to be reversed in place itself.
  • Storage is preferred over performance and not all elements get a separate node of their own

Method 2: Using llist package.

llist is an extension module for CPython providing basic linked list data structures.

Type given below command in your command line:

pip install llist

Below is the implementation of linked list:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing packages
import llist
from llist import sllist,sllistnode
  
# creating a singly 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()
  
# poping 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()

chevron_right


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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.