Representing a set of URLs in a list as a tree structure
<p>I have a list of dicts that stores URLs. It has simply two fields, <code>title</code> and <code>url</code>. Example:</p>

<pre><code>[
  {'title': 'Index Page', 'url': 'http://www.example.com/something/index.htm'}, 
  {'title': 'Other Page', 'url': 'http://www.example.com/something/other.htm'},
  {'title': 'About Page', 'url': 'http://www.example.com/thatthing/about.htm'},
  {'title': 'Detail Page', 'url': 'http://www.example.com/something/thisthing/detail.htm'},
]
</code></pre>

<p>However, I'd to get a tree structure from this list of dicts. I'm looking for something like this:</p>

<pre><code>{ 'www.example.com': 
  [ 
    { 'something': 
      [ 
        { 'thisthing':
          [
            { 'title': 'Detail Page', 'url': 'detail.htm'}
          ]
        },
        [
          { 'title': 'Index Page', 'url': 'index.htm'},
          { 'title': 'Other Page', 'url': 'other.htm'}
        ]
      ]
    },
    { 'thatthing': 
      [ 
        { 'title': 'About Page', 'url': 'about.htm'}
      ]
    }
  ]
}
</code></pre>

<p>My first attempt at this would be a urlparse soup in a bunch of for loops and I'm confident that there's a better and faster way to do this.</p>

<p>I've seen people on SO work magic with list comprehensions, lambda functions, etc. I'm still in the process of figuring it out.</p>

<p>(For Django developers: I'll be using this my Django application. I'm storing the URLs in a model called <code>Page</code> which has two fields <code>name</code> and <code>title</code>)</p>
