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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:36:14+00:00 2026-05-31T10:36:14+00:00

… and every for-loop looked like a list comprehension. Instead of: for stuff in

  • 0

… and every for-loop looked like a list comprehension.

Instead of:

for stuff in all_stuff:
    do(stuff)

I was doing (not assigning the list to anything):

[ do(stuff) for stuff in all_stuff ]

This is a common pattern found on list-comp how-to’s. 1) OK, so no big deal right? Wrong. 2) Can’t this just be code style? Super wrong.

1) Yea that was wrong. As NiklasB points out, the of the HowTos is to build up a new list.

2) Maybe, but its not obvious and explicit, so better not to use it.

I didn’t keep in mind that those how-to’s were largely command-line based. After my team yelled at me wondering why the hell I was building up massive lists and then letting them go, it occurred to me that I might be introducing a major memory-related bug.

So here’er my question/s. If I were to do this in a very long running process, where lots of data was being consumed, would this “list” just continue consuming my memory until let go? When will the garbage collector claim the memory back? After the scope this list is built in is lost?

My guess is yes, it will keep consuming my memory. I don’t know how the python garbage collector works, but I would venture to say that this list will exist until after the last next is called on all_stuff.

EDIT.

The essence of my question is relayed much cleaner in this question (thanks for the link Niklas)

  • 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-31T10:36:15+00:00Added an answer on May 31, 2026 at 10:36 am

    If I were to do this in a very long running process, where lots of data was being consumed, would this “list” just continue consuming my memory until let go?

    Absolutely.

    When will the garbage collector claim the memory back? After the scope this list is built in is lost?

    CPython uses reference counting, so that is the most likely case. Other implementations work differently, so don’t count on it.

    Thanks to Karl for pointing out that due to the complex memory management mechanisms used by CPython this does not mean that the memory is immediately returned to the OS after that.

    I don’t know how the python garbage collector works, but I would venture to say that this list will exist until after the last next is called on all_stuff.

    I don’t think any garbage collector works like that. Usually they mark-and-sweep, so it could be quite some time before the list is garbage collected.

    This is a common pattern found on list-comp how-to’s.

    Absolutely not. The point is that you iterate the list with the purpose of doing something with every item (do is called for it’s side-effects). In all the examples of the List-comp HOWTO, the list is iterated to build up a new list based on the items of the old one. Let’s look at an example:

    # list comp, creates the list [0,1,2,3,4,5,6,7,8,9]
    [i for i in range(10)]
    
    # loop, does nothing
    for i in range(10):
        i  # meh, just an expression which doesn't have an effect
    

    Maybe you’ll agree that this loop is utterly senseless, as it doesn’t do anything, in contrary to the comprehension, which builds a list. In your example, it’s the other way round: The comprehension is completely senseless, because you don’t need the list! You can find more information about the issue on a related question

    By the way, if you really want to write that loop in one line, use a generator consumer like deque.extend. This will be slightly slower than a raw for loop in this simple example, though:

    >>> from collections import deque
    >>> consume = deque(maxlen=0).extend
    >>> consume(do(stuff) for stuff in all_stuff)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
I have this xml <entry id=1008 section=articles> <excerpt><p>&#8230; in Richtung „Aus für Tierversuche. Kosmetik-Fertigprodukte
An Arabic name shall be sent via SOAP. The name is encoded like this:
CGI.escapeHTML is pretty bad, but CGI.unescapeHTML is completely borked. For example: require 'cgi' CGI.unescapeHTML('&#8230;')
what about this one: I want to format the currentTime displayed by a videoPlayer
English is not my native language. I need a software to spellcheck and correct
When I use the list function: el_nino_1974_2000_all <- list() for (k in seq_along(el_nino_start_month)){ el_nino_1974_2000_all[[k]]
I want to run this <!-- This will remove the tag --> <xsl:template name=remove-html>
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.