Python Doubly Linked List
A comprehensive, efficient, and easy-to-use doubly linked list implementation for Python.
Table of Contents
- Features
- Installation
- Quick Start
- API Reference
- Requirements
- Testing
- Contributing
- Performance
- License
Features
- Full doubly linked list functionality - Insert, delete, search, and traverse in both directions
- Pythonic interface - Familiar methods and operators (
len(),in, iteration, etc.) - Memory efficient - Only stores necessary node references
- Type hints - Full type annotation support for better IDE integration
- Well tested - Comprehensive test suite with high coverage
- No dependencies - Pure Python implementation
Why Use This?
When to choose DoublyLinkedList over Python's built-in list:
- ✅ Frequent insertions/deletions at the beginning (O(1) vs O(n))
- ✅ Need bidirectional iteration
- ✅ Working with data that doesn't require random access
- ✅ Memory-conscious applications (no need to pre-allocate)
When to stick with Python's list:
- ✅ Need random access by index (O(1) vs O(n))
- ✅ Numeric computations (better cache locality)
- ✅ Small datasets where performance difference is negligible
Installation
From PyPI (recommended)
pip install python-doubly-linked-list
From source
git clone https://github.com/lejac/python-doubly-linked-list.git
cd python-doubly-linked-list
pip install -e .
Quick Start
from doubly_linked_list import DoublyLinkedList
# Create a new list
dll = DoublyLinkedList()
# Add elements
dll.append(1)
dll.append(2)
dll.prepend(0)
# Access elements
print(dll[0]) # Output: 0
print(dll[-1]) # Output: 2
# Iterate through the list
for item in dll:
print(item)
# Check if element exists
if 1 in dll:
print("Found 1 in the list")
# Get list length
print(len(dll)) # Output: 3
# Remove elements
dll.remove(1)
del dll[0]
# Reverse iteration
for item in reversed(dll):
print(item)
API Reference
DoublyLinkedList Class
Methods
append(value)- Add an element to the end of the listprepend(value)- Add an element to the beginning of the listinsert(index, value)- Insert an element at a specific positionremove(value)- Remove the first occurrence of a valuepop(index=-1)- Remove and return element at index (last by default)clear()- Remove all elements from the listindex(value)- Return the index of the first occurrence of valuecount(value)- Return the number of occurrences of valuereverse()- Reverse the list in placecopy()- Return a shallow copy of the list
Properties
head- Reference to the first nodetail- Reference to the last nodeis_empty- Boolean indicating if the list is empty
Magic Methods
__len__()- Returns the length of the list__getitem__(index)- Get element by index__setitem__(index, value)- Set element by index__delitem__(index)- Delete element by index__contains__(value)- Check if value exists in list__iter__()- Forward iteration__reversed__()- Reverse iteration__str__()- String representation__repr__()- Developer representation
Node Class
The internal Node class represents individual elements:
value- The stored valuenext- Reference to the next nodeprev- Reference to the previous node
Requirements
Runtime Requirements
- Python 3.7+
- No external dependencies
Development/Testing Requirements
- pytest (for running tests)
- pytest-cov (for coverage reports)
- Other dev tools: black, isort, flake8, mypy
Testing
⚠️ Important: Testing requires pytest and other dev dependencies. Make sure to set up your development environment first!
Quick Start
# Using the development utility (recommended)
python dev.py test # Run all tests
python dev.py test-cov # Run tests with coverage
python dev.py all # Full pipeline (format, check, test)
# Manual commands (requires virtual environment with dev dependencies)
pytest tests/ # Run all tests
pytest tests/ --cov=doubly_linked_list --cov-report=html # With coverage
Available Test Suites
- Basic functionality tests:
tests/test_doubly_linked_list.py - Security & robustness tests:
tests/test_security.py - 96%+ code coverage across all modules
Development Environment Setup
# 1. Create virtual environment (if not exists)
python -m venv .venv
# 2. Activate virtual environment
.venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/Mac
# 3. Install in development mode
pip install -e ".[dev]"
# 4. Run development pipeline
python dev.py all # Format, check, and test everything
Troubleshooting
"Import 'pytest' could not be resolved" error?
- Make sure you've activated your virtual environment:
.venv\Scripts\activate(Windows) - Install dev dependencies:
pip install -e ".[dev]" - If no virtual environment exists, create one first:
python -m venv .venv
Tests not found?
- Run tests from the project root directory
- Make sure pytest is installed:
pip list | findstr pytest
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development Setup
- Fork the repository
- Clone your fork:
git clone https://github.com/lejac/python-doubly-linked-list.git
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install development dependencies:
pip install -e ".[dev]"
- Create a branch for your feature:
git checkout -b feature-name
- Make your changes and add tests
- Run the test suite:
pytest
- Submit a pull request
Code Style
This project follows PEP 8 style guidelines. We use:
blackfor code formattingisortfor import sortingflake8for lintingmypyfor type checking
Run code quality checks:
black .
isort .
flake8
mypy doubly_linked_list
Known Issues
None at this time. Please report any issues on the GitHub Issues page.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
lejac
- GitHub: @lejacpCJ
- Email: lejacp@gmail.com
Acknowledgments
- Inspired by Python's built-in
listandcollections.deque
Performance
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Append | O(1) | O(1) |
| Prepend | O(1) | O(1) |
| Insert | O(n) | O(1) |
| Delete | O(n) | O(1) |
| Search | O(n) | O(1) |
| Access | O(n) | O(1) |
Related Projects
- Python Collections - Built-in data structures
- more-itertools - Additional iteration utilities
⭐ If you find this project useful, please consider giving it a star on GitHub!
Metadata
Release files for python-doubly-linked-list-lejacpcj 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| python_doubly_linked_list_lejacpcj-0.1.0.tar.gz | 15.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| python_doubly_linked_list_lejacpcj-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 26.2 kB
Release files / python_doubly_linked_list_lejacpcj-0.1.0.tar.gz
| Download URL | python_doubly_linked_list_lejacpcj-0.1.0.tar.gz |
|---|---|
| Size | 15.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4f3483576736cc781ed79afaed53c5aff6c7699e6cc3e4ef9f4fed26762c3ea4
|
|
BLAKE2b-256 checksum How to use checksums |
6778e9cb635d00bc0f3b5e7247874002ec05694cc3e37676f5586de97b92330c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|
Release files / python_doubly_linked_list_lejacpcj-0.1.0-py3-none-any.whl
| Download URL | python_doubly_linked_list_lejacpcj-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3adfa29f86a60c49d36ada064e98292e4185268ff392efa6aa96983b7634de2
|
|
BLAKE2b-256 checksum How to use checksums |
4c03b24c80e08decbc7075a50d09fb665b4e4e968b7fedf28c4bfda6ac62a203
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.3
|