title: Programmatically create a list of lists in Python 2.7
body: <p>I'm trying to create a list of lists by reading a file of data and I can't get it to work.</p>
  
  <p>To show what I'm trying to do I have the following hard-coded list of lists that works for my purpose.  Each list contains a room name, followed by an x and y coordinate.  This is for a robot navigation program.</p>
  
  <p>Here's the hard-coded listing that works.</p>
  
  <pre><code>nav_Array =[["studio",145.0,155.0],["kitchen",245.0,400.0]]
  print (nav_Array[0])
  print (nav_Array[1])
  print ("\n")
  print (nav_Array[0][0])
  print (nav_Array[0][1])
  print (nav_Array[0][2])
  print ("\n")    
  print (nav_Array[1][0])
  print (nav_Array[1][1])
  print (nav_Array[1][2])
  </code></pre>
  
  <p>Here's the printed output I expected and got:</p>
  
  <pre><code>['studio', 145.0, 155.0]
  ['kitchen', 245.0, 400.0]
  
  studio
  145.0
  155.0
  
  kitchen
  245.0
  400.0
  </code></pre>
  
  <p>Now for my problem.</p>
  
  <p>I have the following text file: I can modify the format of this file if need be, but I'd like to keep each entry on a new line for ease of maintenance.  This is a very short excerpt of this file.  The actual file is going to have many more lists.  Each line being a list.  By the way I've tried adding brackets and that doesn't help as you'll see further down:</p>
  
  <pre><code>studio,145.0,155.5
  kitchen,245.0,400.0
  </code></pre>
  
  <p>So the problem is that I want to read this file into a list of lists like I had manually typed in as nav_Array in the previous example. </p>
  
  <p>Here's what I tried.</p>
  
  <p>First Attempt:</p>
  
  <pre><code>    with open(file_path) as myfile: 
          for line in myfile:
              line = line.rstrip()
              print (line)
              nav_Array.append(line)
          myfile.close()
      print "\n"
      print "print nav_Array: ", nav_Array
  
      print "\n"
      print (nav_Array[0][0])
      print (nav_Array[0][1])
      print (nav_Array[0][2])
      print "\n"
      print (nav_Array[1][0])
      print (nav_Array[1][1])
      print (nav_Array[1][2])
  </code></pre>
  
  <p>The result is the following which is not what I want:</p>
  
  <pre><code>studio,145.0,155.5
  kitchen,245.0,400.0
  
  print nav_Array:  ['studio,145.0,155.5', 'kitchen,245.0,400.0']
  
  s
  t
  u
  
  k
  i
  t
  </code></pre>
  
  <p>That's not creating a list of lists.</p>
  
  <p>So then I tried adding brackets to the original file data like so:</p>
  
  <pre><code>[studio,145.0,155.5]
  [kitchen,245.0,400.0]
  </code></pre>
  
  <p>Then running the same program as above I get the following output which is better, but still not a list of lists.  The following is the output:</p>
  
  <pre><code>[studio,145.0,155.5]
  [kitchen,245.0,400.0]
  
  print nav_Array:  ['[studio,145.0,155.5]', '[kitchen,245.0,400.0]']
  
  [
  s
  t
  
  
  [
  k
  i
  </code></pre>
  
  <p>For some reason single quotes are being put in by the append function.  And that ruins what I'm trying to do.  If I could get rid of those single quotes I'd be home free.</p>
  
  <p>So then I tried the following to the original file format.  All I did here was trying to add the [] in the append function itself, but this gives me precisely the same result as placing the [] in the data file.</p>
  
  <p>The only line difference here is nav_Array.append("["+line+"]")</p>
  
  <pre><code>    with open(file_path) as myfile: 
          for line in myfile:
              line = line.rstrip()
              print (line)
              nav_Array.append("["+line+"]")
          myfile.close()
      print "\n"
      print "print nav_Array: ", nav_Array
  
      print "\n"
      print (nav_Array[0][0])
      print (nav_Array[0][1])
      print (nav_Array[0][2])
      print "\n"
      print (nav_Array[1][0])
      print (nav_Array[1][1])
      print (nav_Array[1][2])
  </code></pre>
  
  <p>And this gives me the same output I got when I added the square brackets to the data file directly.</p>
  
  <pre><code>studio,145.0,155.5
  kitchen,245.0,400.0
  
  print nav_Array:  ['[studio,145.0,155.5]', '[kitchen,245.0,400.0]']
  
  [
  s
  t
  
  [
  k
  i
  </code></pre>
  
  <p>Again that's not what I need.  I need what I got when I typed in the list of lists by hand in the first example at the top of this post. </p>
  
  <p>Just for the record, I've been trying tons of other things as well.  I've been reading a lot of Stack Overflow posts on how to import lists from files, but I haven't been able to find anything that addresses bringing in an actual list of lists. </p>
  
  <p>I'm almost there!  I just can't get rid of the single quotes that appear to be introduced automatically by the append function. </p>
  
  <p>I'm thinking I could write a program to go through and strip out those single quotes as a last resort.  But surely there's a better way to build a list of lists without having to do that. </p>
  
  <p>Thanks for reading. </p>