Lambda expression that creates an index (dictionary with members as keys) of the members of the object in the collection with python
import functools as f
makeindexes = lambda objects:{attr:{str(getattr(x,attr)):x for x in objects if attr in dir(x)} for attr in f.reduce(set.union,(set(y.__dict__.keys()) for y in objects if hasattr(y,"__dict__")),set())}
class A:
def __init__(self,name):
self.name = name
self.id = id(self)
objs = [A(x) for x in ["tama","poti","mii"]]
indexes = makeindexes(objs)
print(indexes.keys())
#dict_keys(['id', 'name'])
print(indexes["name"]["poti"].name)
#poti
If the value is not unique as it is, it cannot be indexed, so I decided to wrap it in a list.
mMakeindexes = lambda objects:{outer:f.reduce(lambda d,o:d[getattr(o,outer)].append(o) or d,objects,{getattr(obj,outer):[] for obj in objects if outer in dir(obj)}) for outer in f.reduce(set.union,(set(y.__dict__.keys()) for y in objects if hasattr(y,"__dict__")),set())}
objs = [A("poti") for x in range(5)]
indexes = mMakeindexes(objs)
print(indexes["name"]["poti"])
#[<__main__.A object at 0x7f0b4f2d7710>, <__main__.A object at 0x7f0b4f2d7790>] [<__main__.A object at 0x7f0b4f2d77d0>, <__main__.A object at 0x7f0b4f2d7850>]
Hmmm, can't you write more beautifully?