Wrapper for a list of objects that allows to create indexes for faster lookups
Project description
# ListLookup
Wrapper for faster lookups in a list of objects/dictionaries.
**ATTENTION** Do not modify list once index are created!
```
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("id", lambda d: d['id'], True)
cities.index("country", lambda d: d['country'])
list(cities.lookup(id=1))
>>> [{"id": 1, "country": "us", name: "Atlanta"}]
list(cities.lookup(country="us", preserve_order=True))
>>> [{"id": 1, "country": "us", name: "Atlanta"}, {"id": 2, "country": "us", name: "Miami"}]
list(cities.lookup(id=2, country="uk"))
>>> []
cities.index('name', lambda d: d['name'])
list(cities.lookup(name=lambda val: val.startswith('B'))
>>> [{"id": 3, "country": "uk", name: "Britain"}, {"id": 4, "country": "ca", "name": "Barrie"}]
```
## Case insensitive index
This is not supported out of the box. You need to use same case for index and lookup values. E.g. use `.lower()`
```
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("country_ci", lambda d: d['country'].lower())
list(cities.lookup(country_ci="UK".lower()))
>>> [{"id": 3, "country": "uk", name: "Britain"}]
```
Wrapper for faster lookups in a list of objects/dictionaries.
**ATTENTION** Do not modify list once index are created!
```
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("id", lambda d: d['id'], True)
cities.index("country", lambda d: d['country'])
list(cities.lookup(id=1))
>>> [{"id": 1, "country": "us", name: "Atlanta"}]
list(cities.lookup(country="us", preserve_order=True))
>>> [{"id": 1, "country": "us", name: "Atlanta"}, {"id": 2, "country": "us", name: "Miami"}]
list(cities.lookup(id=2, country="uk"))
>>> []
cities.index('name', lambda d: d['name'])
list(cities.lookup(name=lambda val: val.startswith('B'))
>>> [{"id": 3, "country": "uk", name: "Britain"}, {"id": 4, "country": "ca", "name": "Barrie"}]
```
## Case insensitive index
This is not supported out of the box. You need to use same case for index and lookup values. E.g. use `.lower()`
```
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("country_ci", lambda d: d['country'].lower())
list(cities.lookup(country_ci="UK".lower()))
>>> [{"id": 3, "country": "uk", name: "Britain"}]
```
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
list-lookup-1.0.3.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for list_lookup-1.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c822fdf9f858ba5fb6aa4c3a6b8a2ffc5abd908e844850f8a81b0721841a9054 |
|
MD5 | 7f9c5138ffc47847084072b95a4b5714 |
|
BLAKE2b-256 | 77ad04c560cfbd4eba186d4be17af6df99d20ea91dc32dd391ae1394ea9db4ef |