Fast ordered dict with deque-like operations at both ends
Project description
DequeDict
Ordered dictionary with O(1) deque operations at both ends.
Features
- O(1) key lookup — like
dict - O(1) pop/peek from both ends — like
deque - O(1) appendleft, move_to_end, delete by key
- Full typing support with
.pyistubs
Installation
pip install dequedict
Usage
from dequedict import DequeDict, DefaultDequeDict
dd = DequeDict([("a", 1), ("b", 2), ("c", 3)])
dd["d"] = 4 # Set
value = dd["a"] # Get
del dd["b"] # Delete
dd.peekleft() # First value
dd.popleft() # Remove first
dd.appendleft("z", 0) # Insert at front
dd.move_to_end("a") # Move to back
# DefaultDequeDict: auto-create values like defaultdict
groups = DefaultDequeDict(list)
groups["a"].append(1)
groups["a"].append(2)
API
| Method | Description |
|---|---|
peekleft() / peek() |
First/last value |
peekleftitem() / peekitem() |
First/last (key, value) |
popleft() / pop() |
Remove and return first/last |
popleftitem() / popitem() |
Remove and return first/last pair |
appendleft(key, value) |
Insert at front |
move_to_end(key, last=True) |
Move to front or back |
at(index) |
Value at position, O(1) amortized (supports negative indexing) |
get, keys, values, items, clear, copy, update, setdefault |
Standard dict ops |
Performance
Mac M1, Python 3.11, C extension:
| Operation | DequeDict | dict | deque | OrderedDict |
|---|---|---|---|---|
| Key lookup | 32 ns | 23 ns | O(n) | 24 ns |
| Peek left | 22 ns | N/A | 23 ns | 59 ns |
| Peek right | 22 ns | N/A | 23 ns | 70 ns |
| Pop left ×100 | 3.7 µs | N/A | 2.1 µs | 7.7 µs |
| Delete by key ×100 | 4.3 µs | 2.8 µs | O(n) | 6.0 µs |
| Insert ×100 | 6.6 µs | 9.5 µs | N/A | 6.8 µs |
Pop is ~1.8x slower than deque due to dict maintenance, but provides O(1) key lookup.
Benchmarks
python benchmarks/benchmark.py
Tests
pip install pytest
python -m pytest tests/ -v
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
dequedict-0.1.4.tar.gz
(17.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dequedict-0.1.4.tar.gz.
File metadata
- Download URL: dequedict-0.1.4.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a58a5852d6cd804d94c9fc415a6606651019010370c0f63a93c2309d30eda0d
|
|
| MD5 |
7b3c41eecb890b490f24e7ae26694ac9
|
|
| BLAKE2b-256 |
75ccb30ea0412b777108d5635dd2f443551caf2b5b22c9dc68e58054b3dc2b0e
|
File details
Details for the file dequedict-0.1.4-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: dequedict-0.1.4-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 24.2 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ec35714490e03363ba75f4f8560d6989bea7c9af126eb85c0bb4ec8a77f5684
|
|
| MD5 |
bfb9379e7a2baf31403fdd0c65bf379e
|
|
| BLAKE2b-256 |
230eab03b45aa8c6431120f898c615ccdf5887d8b30e44eee0ca7e8e4422407f
|