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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:05:12+00:00 2026-06-14T10:05:12+00:00

In Python: Let’s say I have a loop, during each cycle of which I

  • 0

In Python:
Let’s say I have a loop, during each cycle of which I produce a list with the following format:
[‘n1′,’n2′,’n3’]
After each cycle I would like to write to append the produced entry to a file (which contains all the outputs from the previous cycles). How can I do that?

Also, is there a way to make a list whose entries are the outputs of this cycle? i.e.
[[],[],[]] where each internal []=[‘n1′,’n2’,’n3] etc

  • 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-14T10:05:13+00:00Added an answer on June 14, 2026 at 10:05 am

    Writing single list as a line to file

    Surely you can write it into a file like, after converting it to string:

    with open('some_file.dat', 'w') as f:
        for x in xrange(10):  # assume 10 cycles
            line = []
            # ... (here is your code, appending data to line) ...
            f.write('%r\n' % line)  # here you write representation to separate line
    

    Writing all lines at once

    When it comes to the second part of your question:

    Also, is there a way to make a list whose entries are the outputs of this cycle? i.e. [[],[],[]] where each internal []=['n1','n2','n3'] etc

    it is also pretty basic. Assuming you want to save it all at once, just write:

    lines = []  # container for a list of lines
    for x in xrange(10):  # assume 10 cycles
        line = []
        # ... (here is your code, appending data to line) ...
        lines.append('%r\n' % line)  # here you add line to the list of lines
    # here "lines" is your list of cycle results
    with open('some_file.dat', 'w') as f:
        f.writelines(lines)
    

    Better way of writing a list to file

    Depending on what you need, you should probably use one of the more specialized formats, than just a text file. Instead of writing list representations (which are okay, but not ideal), you could use eg. csv module (similar to Excel’s spreadsheet): http://docs.python.org/3.3/library/csv.html

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

Sidebar

Related Questions

Let's say I have the following list of python dictionary: dict1 = [{'domain':'Ratios'},{'domain':'Geometry'}] and
I'm trying to solve the following problem: Say I have a Python script (let's
Let's say I have 2 lists in Python and I want to loop through
Let's say I have the following empty two dimensional array in Python: q =
Let's say I have a Python list that looks like this: list = [
Let's say I have a python script which loads a shared library (SL) through
Let's say I have the following Python code: if conditionOne(): if conditionTwo(): foo =
Let's say I have the following dictionary in a small application. dict = {'one':
Let's say I have a standard Python string (such as one obtained from raw_input()
So let's say i have this python code: def loopForEachFileInDirectory(self, conn): for filename in

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.