Skip to main content

A dictionary that is indexed by insertion order.

Project description

Test PyPI package

Introduction

indexed.IndexedOrderedDict (alias indexed.Dict) is feature compatible with collections.OrderedDict as of Python 3.11 and can be used as a drop in replacement. The main difference is that key, value and item views support accessing elements by their index.

d = indexed.IndexedOrderedDict()
d["first-key"] = "first-value"
d["second-key"] = "second-value"
d["third-key"] = "third-value"

values = d.values()
assert values[2] == "third-value"

assert d.keys().index("second-key") == 1

Features

  • Access keys, values and items by index, e.g., d.keys()[5].

  • Find the index of a key, e.g., d.keys().index("key").

  • Sort keys in place, e.g., d.sort().

Excluding those additions the API is the same as the API of collections.OrderedDict().

Installing

pip install indexed

Performance

Performance is practically on the same order of magnitude as the built in collections.OrderedDict, with exceptions in bold:

d

collections.OrderedDict

indexed.IndexedOrderedDict

Operation

Avergage

Worst case

Average

Worst case

d.copy()

O(n)

O(n)

O(n)

O(n)

d[key]

O(1)

O(n)

O(1)

O(n)

d[key] = value

O(1)

O(n) [1]

O(1)

O(n) [1]

del d[key]

O(1)

O(n)

O(n)

O(n)

d.keys()[i]

O(n) [2]

O(n) [2]

O(1)

O(1)

d.values()[i]

O(n) [3]

O(n) [3]

O(1)

O(n)

d.items()[i]

O(n) [3]

O(n) [3]

O(1)

O(n)

d.keys().index(x)

O(n) [3]

O(n) [3]

O(n)

O(n)

License

This library is derived from CPython’s collections.OrderedDict and licensed under the PSFL. See the LICENSE file for the full license text.

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

indexed-1.3.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

indexed-1.3.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file indexed-1.3.0.tar.gz.

File metadata

  • Download URL: indexed-1.3.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for indexed-1.3.0.tar.gz
Algorithm Hash digest
SHA256 6a0dd1f164db2eef6f9983bf1c5302d4b250a05b784f15c4c3f436d8778243d9
MD5 ec17f5f3d2d10cbf84a729ea4363c195
BLAKE2b-256 0791ebbff25d520fd4c904fcec67a1e4800c6e37c17f4c64057693e38aa2455b

See more details on using hashes here.

File details

Details for the file indexed-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: indexed-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for indexed-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a35db8644bef9273be710f5f06b5ffe71b8699d9212593cbae422b5e3c5f64c6
MD5 dbf36950704c2e4cd1a63ee5233d43d0
BLAKE2b-256 628be5c5af4bb7f87409afd3addd3612bc471b2e141987d3edf64c7693cae7c4

See more details on using hashes here.

Supported by

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