Tree of lists question

Hi
I want to create c# script (or Python) but i don’t find a solution
The idea like that:

if (x == true)
   list.add(data)
   //and continue adding ...
if (x = false)
    //finish the previous list and create new one

// call the function again ...
if (x == true)
    //...............
if (x = false)
    //...............

// the final output is a trees of all lists
// output = tree of the lists created

I am sure this need loop but i don’t find a way