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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:39:57+00:00 2026-06-08T09:39:57+00:00

Note: I have edited the original question based on comments and answers. My question

  • 0

Note: I have edited the original question based on comments and answers.

My question is if a large quantity of Python data is input into a program, how can that data be made lazy, so memory does not overflow?

For example, if a list is built by reading in a file and appending each line or portion of a line to a list, is that list lazy? In other words, can a list be appended to and the list be lazy? Is appending to a list reading the entire file into memory?

I understand that if I wanted to walk through that list, I would write a generator function to keep the access lazy.

What is triggering this question is this recent SO post

If this data were coming from a database table with 10M rows, like one of our MySQL daily water meter reads tables, I would not use the mysqldb fetchall() command without knowing how to make the data lazy. Instead, I would read one row at a time.

But what If I did want the contents of that data in memory as a lazy sequence? How would I do it in Python?

Given that I am not presenting source code with a specific problem, the answer I’m looking for is a pointer or pointers to a place in the Python documentation or somewhere else to solve this problem.

Thanks.

  • 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-08T09:39:59+00:00Added an answer on June 8, 2026 at 9:39 am

    The basic idea of “lazy” code is that the code does not get data until it needs the data.

    For example, suppose I am writing a function to copy a text file. It would not be lazy to read the entire file into memory, then write the entire file. It also would not be lazy to use the .readlines() method to build a list out of all the input lines. But it would be lazy to read one line at a time and then write each line after reading.

    # non-lazy
    with open(input_fname) as in_f, open(output_fname, "w") as out_f:
        bytes = in_f.read()
        out_f.write(bytes)
    
    # also non-lazy
    with open(input_fname) as in_f, open(output_fname, "w") as out_f:
        lines = in_f.readlines()
        for line in lines:
            out_f.write(line)
    
    # lazy
    with open(input_fname) as in_f, open(output_fname, "w") as out_f:
        for line in in_f:  # only gets one line at a time
            out_f.write(line) # write each line as we get it
    

    To help make your code lazy, Python lets you use “generators”. Functions written using the yield statement are generators. For your database example, you could write a generator that would yield up one row at a time from the database, and then you could write code like this:

    def db_rows(database_name):
        # code to open the database goes here
        # write a loop that reads rows
            # inside the loop, use yield on each row
            yield row
        # code to close the database goes here
    
    for row in db_rows(database_name):
        # do something with the row
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Edited based on responses to receive more appropriate answers. I have a collection
Note: question has been edited to stay in sync with what I have tried
NOTE: EDITED The real-world situation is a series of events that each have two
NOTE: I have solved the majority of this problem but have run into a
Note I have completely re-written my original post to better explain the issue I
(Please note that I have seen a similar question on StackOverflow recently, however I
Note: I edited this question to make it easier for other people with the
[Editor's note: I have edited the title to try to make this useful to
I have a edited RESTful wizard based upon Shoulders of Giants | A RESTful
NOTE: I have read Routing From the Inside Out AND the Engine Yard blog

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.