list of list but specific representations in python
<p>I don't have much experience about lists in Python. So basically, I want to create N list inside one huge list. Reading from a file that looks like this:</p>

<pre><code>Fail = h yes
Sucess = h no
</code></pre>

<p>This is an example of 2 lines in the file, and the file contains N line, thus I want to create a list for each line such that it should be:</p>

<pre><code>List of list = [
        ["Fail", ("h", "yes")],
        ["Sucess", ("h", "no")]],]
</code></pre>

<p>And in case a line of:</p>

<pre><code>Fail = h 
</code></pre>

<p>with only one output, its list should be like:</p>

<pre><code>["Fail", ("h", )),]
</code></pre>

<p>My code doesn't work for now,:</p>

<pre><code>with open('file.txt') as f:
     for l in f:
         l_rule = l.split()
        # print(l_rule)
         G_list.append(l_rule[0])
         length= len(l_rule)
         if length == 3:

                 m = "(" + l_rule[2]+ "," +")"
                 G_list.append(m)


         else:


                 m = "(" + l_rule[2]+","+l_rule[3] +")"
                 G_list.append(m)

         print(G_list)
         listoflist.append(G_list)

print(listoflist)
</code></pre>

<p>And it doesn't return what I need as I explained above. I appreciate the help.</p>

<p>Try this:</p>

<pre><code>def format_data(line):
    lst = line.split()
    return lst[0], tuple(lst[2:])

with open('file.txt') as f:
    for l in f:
        G_list.append(format_data(l))
listoflist.append(G_list)
</code></pre>

<p>or you can do a one_liner inside your <code>with</code> statement.</p>

<pre><code>with open('file.txt') as f:
    listoflist = [format_data(l) for l in f]
</code></pre>

python list