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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:43:38+00:00 2026-05-14T15:43:38+00:00

I’m doing some performance-critical Python work and want to create a function that removes

  • 0

I’m doing some performance-critical Python work and want to create a function that removes a few elements from a list if they meet certain criteria. I’d rather not create any copies of the list because it’s filled with a lot of really large objects.

Functionality I want to implement:

def listCleanup(listOfElements):
    i = 0
    for element in listOfElements:
        if(element.meetsCriteria()):
            del(listOfElements[i])
        i += 1
    return listOfElements

myList = range(10000)
myList = listCleanup(listOfElements)

I’m not familiar with the low-level workings of Python. Is myList being passed by value or by reference?

How can I make this faster?

Is it possible to somehow extend the list class and implement listCleanup() within that?

myList = range(10000)
myList.listCleanup()

Thanks-

Jonathan

  • 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-05-14T15:43:38+00:00Added an answer on May 14, 2026 at 3:43 pm

    Python passes everything the same way, but calling it “by value” or “by reference” will not clear everything up, since Python’s semantics are different than the languages for which those terms usually apply. If I was to describe it, I would say that all passing was by value, and that the value was an object reference. (This is why I didn’t want to say it!)

    If you want to filter out some stuff from a list, you build a new list

    foo = range(100000)
    new_foo = []
    for item in foo:
        if item % 3 != 0: # Things divisble by 3 don't get through
            new_foo.append(item)
    

    or, using the list comprehension syntax

     new_foo = [item for item in foo if item % 3 != 0]
    

    Python will not copy the objects in the list, but rather both foo and new_foo will reference the same objects. (Python never implicitly copies any objects.)


    You have suggested you have performance concerns about this operation. Using repeated del statements from the old list will result in not code that is less idiomatic and more confusing to deal with, but it will introduce quadratic performance because the whole list must be reshuffled each time.

    To address performance:

    • Get it up and running. You can’t figure out what your performance is like unless you have code working. This will also tell you whether it is speed or space that you must optimize for; you mention concerns about both in your code, but oftentimes optimization involves getting one at the cost of the other.

    • Profile. You can use the stdlib tools for performance in time. There are various third-party memory profilers that can be somewhat useful but aren’t quite as nice to work with.

    • Measure. Time or reprofile memory when you make a change to see if a change makes an improvement and if so what that improvement is.

    • To make your code more memory-sensitive, you will often want a paradigm shift in how you store your data, not microoptimizastions like not building a second list to do filtering. (The same is true for time, really: changing to a better algorithm will almost always give the best speedup. However, it’s harder to generalize about speed optimizations).

      Some common paradigm shifts to optimize memory consumption in Python include

      1. Using Generators. Generators are lazy iterables: they don’t load a whole list into memory at once, they figure out what their next items are on the fly. To use generators, the snippets above would look like

        foo = xrange(100000) # Like generators, xrange is lazy
        def filter_divisible_by_three(iterable):
            for item in foo:
                if item % 3 != 0:
                    yield item
        
        new_foo = filter_divisible_by_three(foo)
        

        or, using the generator expression syntax,

        new_foo = (item for item in foo if item % 3 != 0)
        
      2. Using numpy for homogenous sequences, especially ones that are numerical-mathy. This can also speed up code that does lots of vector operations.

      3. Storing data to disk, such as in a database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It probably means you need to rebuild statistics. This is… May 14, 2026 at 8:58 pm
  • Editorial Team
    Editorial Team added an answer The element itself is protected; you need to use the… May 14, 2026 at 8:58 pm
  • Editorial Team
    Editorial Team added an answer To escape a character, use the backslash. @"\""; And to… May 14, 2026 at 8:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.