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 345205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:03:44+00:00 2026-05-12T11:03:44+00:00

I need a collection of objects which can be looked up by a certain

  • 0

I need a collection of objects which can be looked up by a certain (unique) attribute common to each of the objects. Right now I am using a dicitionary assigning the dictionary key to the attribute.
Here is an example of what I have now:

class Item():
    def __init__(self, uniq_key, title=None):
        self.key = uniq_key
        self.title = title

item_instance_1 = Item("unique_key1", title="foo")
item_instance_2 = Item("unique_key3", title="foo")
item_instance_3 = Item("unique_key2", title="foo")

item_collection = {
        item_instance_1.key: item_instance_1,
        item_instance_2.key: item_instance_2,
        item_instance_3.key: item_instance_3
        }

item_instance_1.key = "new_key"

Now this seems a rather cumbersome solution, as the key is not a reference to the attribute but takes the value of the key-attribute on assignment, meaning that:

  • the keys of the dictionary duplicate information already present in form of the object attribute and
  • when the object attribute is changed the dictionary key is not updated.

Using a list and iterating through the object seems even more inefficient.

So, is there more fitting data structure than dict for this particular case, a collection of objects giving me random access based on a certain object attribute?

This would need to work with Python 2.4 as that’s what I am stuck with (at work).

If it hasn’t been obvious, I’m new to Python.

  • 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-05-12T11:03:44+00:00Added an answer on May 12, 2026 at 11:03 am

    There is actually no duplication of information as you fear: the dict’s key, and the object’s .key attribute, are just two references to exactly the same object.

    The only real problem is “what if the .key gets reassigned”. Well then, clearly you must use a property that updates all the relevant dicts as well as the instance’s attribute; so each object must know all the dicts in which it may be enregistered. Ideally one would want to use weak references for the purpose, to avoid circular dependencies, but, alas, you can’t take a weakref.ref (or proxy) to a dict. So, I’m using normal references here, instead (the alternative is not to use dict instances but e.g. some special subclass — not handy).

    def enregister(d, obj):
      obj.ds.append(d)
      d[obj.key] = obj
    
    class Item(object):
        def __init__(self, uniq_key, title=None):
            self._key = uniq_key
            self.title = title
            self.ds = []
    
        def adjust_key(self, newkey):
            newds = [d for d in self.ds if self._key in d]
            for d in newds:
              del d[self._key]
              d[newkey] = self
            self.ds = newds
            self._key = newkey
    
        def get_key(self):
            return self._key
    
        key = property(get_key, adjust_key)
    

    Edit: if you want a single collection with ALL the instances of Item, that’s even easier, as you can make the collection a class-level attribute; indeed it can be a WeakValueDictionary to avoid erroneously keeping items alive, if that’s what you need. I.e.:

    class Item(object):
    
        all = weakref.WeakValueDictionary()
    
        def __init__(self, uniq_key, title=None):
            self._key = uniq_key
            self.title = title
            # here, if needed, you could check that the key
            # is not ALREADY present in self.all
            self.all[self._key] = self
    
        def adjust_key(self, newkey):
            # "key non-uniqueness" could be checked here too
            del self.all[self._key]
            self.all[newkey] = self
            self._key = newkey
    
        def get_key(self):
            return self._key
    
        key = property(get_key, adjust_key)
    

    Now you can use Item.all['akey'], Item.all.get('akey'), for akey in Item.all:, and so forth — all the rich functionality of dicts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Often times I need a collection of non-sequential objects with numeric identifiers. I like
I have a collection of domain objects that I need to convert into another
Suppose you have a collection of a few hundred in-memory objects and you need
I have a collection of HTML documents for which I need to parse the
I need to create feature which will iterate through all subsites of site collection
I'm using garbage collection in Objective-C 2.0. Do I need to retain properties? Eg.
I have a collection which contains two type of objects A & B. Class
I need to solve the following question which i can't get to work by
I have a User object which has a collection of Transaction objects under it.
I need to convert a passed in argument (single object or collection) to an

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.