Code Monkey home page Code Monkey logo

dictanykey's Introduction

DictAnyKey: Python Dictionary That Can Use Any Key

PyPI Latest Release

What is it?

DictAnyKey is a Python package that provides a dictionary like object that can use unhashable keys (such as list and dict) as well as hashable keys.

Main Features

Here are just a few of the things that DictAnyKey does well:

  • Use unhashable objects as keys, such as list and dict but with slower retrieval.
  • Stores and retrieves values using hashable keys at same speed as built in dict.
  • Maintains order of insertion just like built in dict.

Where to get it

The source code is currently hosted on GitHub at: https://github.com/eddiethedean/dictanykey

# PyPI
pip install dictanykey

Dependencies

  • [python >= 3.6]

Example

from dictanykey import DictAnyKey

# Start with empty DictAnyKey
d = DictAnyKey()

# Add value with unhashable key
d[[1, 2]] = 'one two'

# Add value with hashable key
d[1] = 'one'

# Get value with key
d[[1, 2]] -> 'one two'

str(d) -> '{[1, 2]: "one two", 1: "one"}'

# Delete items with del
del d[[1, 2]]

# Start with filled in DictAnyKey, use list of tuples
d = DictAnyDict([([2, 2], 'two two'), (2, 'two')])

str(d) -> '{[2, 2]: "two two", 2: "two"}'

# Has keys, values, and items methods (all results are iterable)
d.keys() -> DictKeys([[2, 2], 2])
d.values() -> DictValues('two two', 'two')
d.items() -> DictItems([([2, 2], 'two two'), (2, 'two')])

# check for key membership
[2, 2] in d -> True
[3, 4] in d -> False

# Has get method with default parameter
d.get(5, default='Missing') -> 'Missing'
d.get([2, 2], default='Missing') -> 'two two'

dictanykey's People

Watchers

James Cloos avatar Odos Matthews avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.