Skip to main content

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 indexes 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"}]

Multiple pointers per item

You can have same record be referenced by multiple values. In the following example "uk" and "uk2" correspond to the same record because they are returned as alternatives during indexing:

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_alt", lambda d: [d['country'], "%s2" % d['country']], multiple=True)

list(cities.lookup(country_alt="uk"))
>>> [{"id": 3, "country": "uk", name: "Britain"}]

list(cities.lookup(country_alt="uk2"))
>>> [{"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.

Files for list-lookup, version 1.0.7
Filename, size File type Python version Upload date Hashes
Filename, size list_lookup-1.0.7-py2.py3-none-any.whl (4.5 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size list-lookup-1.0.7.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page