
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
DLLIST Class of LList Module in Python
The dllist is a class of the llist module in Python that is used to implement a doubly linked list that has the functionality for insertion, deletion, and traversal of elements. The dllist class provides methods for adding, removing, and iterating over the list in both directions. In this article, we will understand the dllist class in detail and its methods.
Creating dllist object
To create a dllist object, we need to first import the llist module from the pyllist package. We can then use the dllist class constructor to create a new instance of a doubly-linked list. The below code will create an empty doubly-linked list.
from pyllist import dllist # create an empty doubly-linked list my_list = dllist()
Adding elements to a dllist
We can easily add elements to the dllist using the append() method which takes the element as an argument and adds the element to the end of the list.
my_list.append(1) my_list.append(2) my_list.append(3)
Accessing elements in a dllist
We can access the elements of the dllist using bracket notation, similar to how we access the elements of a list. Also, we can access all the elements in the list using for loop.
from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) print(my_list[0]) print(my_list[2]) for item in my_list: print(item)
Output
1 3 1 2 3
Removing an element from dllist
We can use the pop method to remove the element at the last index in the list.Also, we can use the remove method to remove the first occurrence of a specific element from the list.
Example
from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) value = my_list.pop() print(value) node= my_list.nodeat(1) my_list.remove(node) print(my_list)
Output
3 dllist([1])
Other Methods of dllist
In addition to the methods described above, the dllist class also provides several other useful methods ?
first ? Returns the first element of the list.
last ? Returns the last element of the list.
index ? Returns the index of the first occurrence of a specific element in the list.
from pyllist import dllist # create an empty doubly-linked list my_list = dllist() my_list.append(1) my_list.append(2) my_list.append(3) print(my_list.first) print(my_list.last)
Output
dllistnode(1) dllistnode(3)
Conclusion
In this article, we discussed the dllist class of the llist module in python. Dllist class is used to implement a doubly linked lists in python.It provides various methods for adding, removing and iterating over the element in the list.