Reputation: 3999
Say i have a list of objects. Say the object has a data member 'name'. Say I want to get a sub list of all objects that have a certain value of 'name'. Any elegant way to do this beyond:
class Person(Base):
name = Column(Text)
p1 = Person(name="joe")
p2 = Person(name="jill")
plst = [ p1, p2 ]
name_test = "jill"
found_people = list()
for person in plst:
if person.name == name_test:
found_people.append(person)
looking for an elegant solution that is not so verbose. not sure if this python code compiles or not :)
Upvotes: 0
Views: 51
Reputation: 81936
You could use a list comprehension.
class Person(Base):
name = Column(Text)
plist = [Person(name="joe"), Person(name="jill")]
found_people = [person for person in plist if person.name == "jill"]
Upvotes: 2