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

  • SEARCH
  • Home
  • 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 7834869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:22:01+00:00 2026-06-02T13:22:01+00:00

I have a list of objects, each of the same kind. Each object has

  • 0

I have a list of objects, each of the same kind.

Each object has its own list of objects (usually just 5-10 items)

What I used to do was:

for o in main_object_list:
    obj_list = o.get_this_object_list()
    for i in obj_list:
        if i in main_object_list:
            //do something with i

While this approach works, when main_object_list has, say, 100.000 elements,
it goes horribly slow.

My workaround has been this:

for o in main_object_list:
    o.flag = True 

for o in main_object_list:
    obj_list = o.get_this_object_list()
    for i in obj_list:
        if i.flag:
            //do something with i

It goes several orders of magnitude faster (from 22 minutes to as little as 17 secs)
but I suspect there may be a different, and better, approach. Moreover, this example works just becaue each object has a flag property, and by the way it is not so
elegant to use a flag that may well have been setted/unsetted in other functions
(if this function is called in the body of a parent function which uses the same
flag mechanisms, this would mess everything up, setting every objects flag)

Is there a more correct pythonesque way to quickly check if an object is in
main_object_list?

  • 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-02T13:22:02+00:00Added an answer on June 2, 2026 at 1:22 pm

    If you want to use your own flag, you could do:

    for o in main_object_list:
        o.my_special_flag = True 
    
    for o in main_object_list:
        obj_list = o.get_this_object_list()
        for i in obj_list:
            if hasattr(i, 'my_special_flag'):
    

    Otherwise set.intersection is as fast as it gets:

    main_object_set = set(main_object_list)
    
    for o in main_object_list:
        obj_list = o.get_this_object_list()
        objs_in_main_list = main_object_set.intersection(obj_list)
        for i in objs_in_main_list:
            //do something with i
    

    Or:

    main_object_set = set(main_object_list)
    
    objs_in_main_list = set().update(
                               *(o.get_this_object_list() for o in main_object_list))
    objs_in_main_list.intersection_update(main_object_set)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects. Each object has an integer quantity and a
Background I have a list . This list has many objects. Each object has
I have a manager class maintaining a list of objects. Each Object has a
I have a list of objects and each and every object in the list
I have a list of objects and in each object they contain a list
I have a List, it has Foo objects, and each Foo has a start
Hi I have created a List of Objects. Each object contains a Set. I
I am iterating through a list of certain objects (each object has two attributes
I have a list of objects, each containing an Id, Code and Description. I
I have two list of different objects : List<Report> List<Newsletter> each having a 'created

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.