Skip to main content

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 .pyi stubs

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
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.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dequedict-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (21.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file dequedict-0.1.0.tar.gz.

File metadata

  • Download URL: dequedict-0.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for dequedict-0.1.0.tar.gz
Algorithm Hash digest
SHA256 54f255f6da656a14497f1bf05f5823db4ba17fcad6c45e2b61e21c26839a8ef1
MD5 69360d9c35beef347a0a390ecbf88566
BLAKE2b-256 68005f3a919c10afe45851aed98db41dfba1b54e231be87b8dd849fdeef13727

See more details on using hashes here.

File details

Details for the file dequedict-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dequedict-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 36e4492170266dd506848ee697de7c73cfab54be8b52e33ad3611fabe216a0ee
MD5 c579a021833306e20b8d39f20606a735
BLAKE2b-256 0b08702b7dec27a275b6e210a9d20882fa015c0d98292e22b4bb06e04eb405f7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page