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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:30:44+00:00 2026-06-13T11:30:44+00:00

In python, I am working on a project that regularly involves computing how many

  • 0

In python, I am working on a project that regularly involves computing how many objects with some given properties match certain conditions. I can see how to do this with lists of tuples or objects or a database, but I wonder if filtering a list of objects this way is the ‘obvious pythonic’ way to do it.

The options I thought of look like this:

list_of_all = [object_type(property0, property1, ...), ...]
number_of_matches = len(filter(object_type.property2_test(property2),
    filter(object_type.property1_getter, list_of_all)

list_of_all = [object_type(property0, property1, ...), ...]
number_of_matches = len([0 for candidate in list_of_all
    if candidate.property1 and candidate.property2 == property2])

list_of_all = [(property0, property1, ...), ...]
number_of_matches = len([0 for candidate in list_of_all
    if candidate[1] and candidate[2] == property2])

db_cursor.execute("""CREATE TABLE table_of_all
    (property0 INTEGER, property1 INTEGER, ...)""")
number_of_matches = len(db_cursor.execute("""SELECT 1 FROM table_of_all
    WHERE property1 = 1 AND property2 = ?""", (property2,)).fetchall())

Using the last two in context of my code, timeit tells me that there is so significant difference.

$ python -m timeit -n 100 'import with_db' | tail -n1
100 loops, best of 3: 0.751 usec per loop
$ python -m timeit -n 100 'import with_list' | tail -n1
100 loops, best of 3: 0.761 usec per loop

with both values somewhere between 0.751 and 0.811 every time.

In general, my use case consists of a few hundred (for testing purposes) up to at least 40000 objects. They are used in a simulation loop (no I/O to wait for). Every loop iteration consists of around 50 of these lookups, possibly finding the maximum of one property, and the update of two objects, but if there are other use cases where there is a best solution, that would also be interesting.

Is there a distinct better solution for this type of task, or is there a distinct type of task where chosing any of these solutions makes a difference?

  • 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-13T11:30:45+00:00Added an answer on June 13, 2026 at 11:30 am

    You can let the database do the counting:

    SELECT COUNT(*) FROM table_of_all
        WHERE property1 = 1 AND property2 = ?
    

    In Python you can avoid creating an intermediate list by using sum and a generator expression:

    sum(1 for candidate in list_of_all
        if candidate.property1 and candidate.property2 == property2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that involves uploading an image to tumblr from Python.
I'm in the process of working on programming project that involves some pretty extensive
I'm working on a project that involves taking some source code and boiling it
I need to have an at-home project now that I'm working on Python/Django at
I'm working on a fairly large project in Python that requires one of the
I'm working on a project that gets callbacks from some other sites' API. I
As a way to learn RabbitMQ and python I'm working on a project that
I'm running into an issue while unit-testing a Python project that I'm working on
The project that I'm currently working on involves translating a website that was written
I am working on a big project that involves a lot of web based

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.