title: How to write nested list back to text file so that there is one list per line?
body: <h1>Question:</h1>
  
  <p>How to write a nested list from a variable into a text file so that each list inside the nested list is written into it's own line?</p>
  
  <p>The list is needed to be compiled back into the text file in exactly the same format as it was before pulling it out with the <code>f.readfiles()</code> function.</p>
  
  <p>Other than variable based solutions and multi-variable solutions are also accepted. <code>csv module</code> is also an option but I'd rather learn the logic of this first without it.</p>
  
  <h2>Introduction to the problem</h2>
  
  <p>Assume there is a <code>text file</code> that contains <code>list</code> in each and every line like this:</p>
  
  <pre><code>['some_text','foo', some_int]
  ['some_text','foo', some_int]
  ['some_text','foo', some_int]
  ...
  </code></pre>
  
  <p>Each list contains several items. All the lines are wanted to be <code>read</code> from the text file into a one big nested list and stored into a <code>variable</code> like this:</p>
  
  <pre><code>variable = [['some_text','foo', some_int],
              ['some_text','foo', some_int],
              ['some_text','foo', some_int]]
  </code></pre>
  
  <p>The format of the information stored into the variable above is irrelevant. The list stored into the variable is now edited with some <a href="https://docs.python.org/3.6/tutorial/datastructures.html" rel="nofollow noreferrer">list tools</a>. When editing is complete, the variable containing the edited list is written back to the same text file again, overwriting all the data with new changed data like this:</p>
  
  <pre><code>['some_text','bar', some_int]
  ['edited_tx','foo', some_int]
  ['some_text','foo', edtd_int]
  ...
  </code></pre>
  
  <h2>Reading the lines</h2>
  
  <p>I managed to read the lines with this code:</p>
  
  <pre><code>with open("my_lists.txt") as f:
      listed_text = f.readlines()
  </code></pre>
  
  <p>All the text inside the text file is now stored into a variable named 'listed_text' which by then is being edited with some <a href="https://docs.python.org/3.6/tutorial/datastructures.html" rel="nofollow noreferrer">list tools</a> prefered.</p>
  
  <p>More about the code above is here:</p>
  
  <p><a href="https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list">How do I read a file line-by-line into a list?</a></p>
  
  <h2>Incomplete code example for reading and writing the file</h2>
  
  <p>This is incomplete and inoperative coding example of what should have been done with the lists.</p>
  
  <pre><code># Let us open 'my_lists.txt' and read lines
  # into a variable as a list.
  
  
  def open_file():
      with open("my_lists.txt") as f:
          listed_text = f.readlines()
      list_item_editor(listed_text)
  
  # Now let us replace 'foo' every index(1) in
  # nested lists with word 'bar' with
  # this imaginary editor 'list_item_editor()'.
  
  
  def list_item_editor(argument):
      edited_list = []
      for item in nested_list:
          nested_list[item][i] = "bar"
      ...
      ...
      write_file(edited_list)
  
  # Now we write the edited list back to 'my_lists.txt'
  # by replacing the whole contents with new edited list.
  
  
  def write_file(arg):
      with open("my_lists.txt", "w") as write_file:
          write_file.write(arg)
  </code></pre>
  
  <h2>Preferred output</h2>
  
  <p>This is example of the preferred output. Each item (list) inside the nested list is written into it's own line. The 'new line mark' <code>\n</code> is not visible inside text file.</p>
  
  <pre><code># This is text_file.txt containing edited lists
  # New line inserted after every list
  
  ['some_text','bar', some_int]
  ['edited_tx','foo', some_int]
  ['some_text','foo', edtd_int]
  ['some_text','bar', some_int]
  ['edited_tx','foo', some_int]
  ['some_text','foo', edtd_int]
  ['some_text','bar', some_int]
  ['edited_tx','foo', some_int]
  ['some_text','foo', edtd_int]
  </code></pre>