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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:26:21+00:00 2026-06-05T09:26:21+00:00

lets assume the following simple Object: class Mock: def __init__(self, name, age): self.name =

  • 0

lets assume the following simple Object:

class Mock:
    def __init__(self, name, age):
        self.name = name
        self.age = age

then I have a list with some Objects like this:

myList = [Mock("Dan", 34), Mock("Jack", 30), Mock("Oli", 23)...]

Is there some built-in feature where I can get all Mocks with an age of ie 30?
Of course I can iterate myself over them and compare their ages, but something like

find(myList, age=30)

would be nice. Is there something like that?

  • 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-05T09:26:22+00:00Added an answer on June 5, 2026 at 9:26 am

    You might want to pre-index them –

    from collections import defaultdict
    
    class Mock(object):
        age_index = defaultdict(list)
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
            Mock.age_index[age].append(self)
    
        @classmethod
        def find_by_age(cls, age):
            return Mock.age_index[age]
    

    Edit: a picture is worth a thousand words:

    enter image description here

    X axis is number of Mocks in myList, Y axis is runtime in seconds.

    • red dots are @dcrooney’s filter() method
    • blue dots are @marshall.ward’s list comprehension
    • green dots hiding behind the X axis are my index 😉
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume I had the following Model class A(Models.model): def __init__(self,data): B(a=self,data=data).save() class B(Models.model):
Lets assume we have the following code: abstract class Base1 { protected int num;
Lets assume i have a dataview with the following itemTpl itemTpl: [ '<div class=category_entry_inner>',
Let's say I have 5 separate assemblies with the following (assume the class name
Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
Lets assume i have a query like the following $query_search = SELECT * FROM
Let's assume we have the following Map index: public class CommentsIndex : AbstractIndexCreationTask<Post> {
I have a simple 1:many aggregate relationship, lets say: public class Parent { public
Lets assume that I have the following configuration in my conf/InjectionConfig.groovy file: x {

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.