Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8740061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:03:50+00:00 2026-06-13T11:03:50+00:00

I am looking for the best way to create a list in python that

  • 0

I am looking for the best way to create a list in python that creates hashed indexes (dicts) for all the properties of the objects put into the list.

>>> foo = IndexingList([{ 'id': 1, 'name': 'cat' }, { 'id': 2, 'name': 'dog' }])
>>> foo[0]
{'id': 1, 'name': 'cat'}

>>> foo.findall('id', 2)
[{'id': 2, 'name': 'dog'}]

>>> foo += {'id': 3, 'name': 'dog'}
>>> foo.findall('name', 'dog')
[{'id': 2, 'name': 'dog'}, {'id': 3, 'name': 'dog'}]

I imagine the data structure of the IndexingList would then look like this:

{
    'items': [
        { 'id': 1, 'name': 'cat' }, 
        { 'id': 2, 'name': 'dog' }
    ],
    'indexes': {
        'id': {
            1: [{ 'id': 1, 'name': 'cat' }],
            2: [{ 'id': 2, 'name': 'dog' }]
        },
        'name': {
            'cat': [{ 'id': 1, 'name': 'cat' }],
            'dog': [
                { 'id': 2, 'name': 'dog' },
                { 'id': 3, 'name': 'dog' }
            ]
        }
    }
}

where the objects within the ‘indexes’ nodes refer to the same objects in ‘items’.

I think property values that are themselves objects could receive unique index-keys by using str(property) to obtain something to stick in ‘indexes’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:03:51+00:00Added an answer on June 13, 2026 at 11:03 am

    This is actually pretty easy to do using some collections.defaultdict()s – although you might consider using an actual database if you are using this a lot.

    from collections import defaultdict
    from functools import partial
    
    class IndexingList:
        def __init__(self, items):
            self.items = []
            self.indices = defaultdict(partial(defaultdict, list))
            self.extend(items)
    
        def append(self, item):
            try:
                for index, value in item.items():
                    self.indices[index][value].append(item)
            except AttributeError as e:
                raise ValueError("All children of an IndexingList must be "
                                 "dict-like. '{0}' is not.".format(item)) from e
            self.items.append(item)
    
        def extend(self, iterable):
            for item in iterable:
                self.append(item)
    
        def __iadd__(self, other):
            self.extend(other)
            return self
    
        def __getitem__(self, item):
            return self.items[item]
    
        def __setitem__(self, item, value):
            self.items[item] = value
    
        def __delitem__(self, item):
            del self.items[item]
            for index, value in item.items():
                self.indices[index][value].remove(item)
    
        def find_all(self, index, value):
            return self.indices[index][value]
    
        def __repr__(self):
            return repr(self.items)
    

    Used like so:

    >>> foo = IndexingList([{ 'id': 1, 'name': 'cat' }, { 'id': 2, 'name': 'dog' }])
    >>> foo[0]
    {'id': 1, 'name': 'cat'}
    >>> foo.find_all("id", 2)
    [{'id': 2, 'name': 'dog'}]
    >>> foo += [{'id': 3, 'name': 'dog'}]
    >>> foo.find_all('name', 'dog')
    [{'id': 2, 'name': 'dog'}, {'id': 3, 'name': 'dog'}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for the best way to create a unique ID as a String
I'm looking for the best way to create a custom border around an HTML
I'm looking to take a string and create a list of strings that build
I am looking for the best way to create ajax enabled subforms from items
I'm looking for the best way to deal with a modifiable list of application
I'm looking for the best way to display all rows from two tables while
I'm looking for best way of using session within zf application. At first I
Looking for the best way to set-up an iPhone project in XCode ... namely:
I am looking for the best way to do a One Time Login for
I'm looking for the best way to get quantity of my unique visitors on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.