title: Traversing and modifying a tree-like list of dict structure
body: <p>I have a structure that looks like this:</p>
  
  <pre><code>[ {'id': 4, 'children': None},
    {'id': 2, 'children': 
      [ {'id': 1, 'children':
          [ {'id': 6, 'children': None},
            {'id': 5, 'children': None} ]
        },
        {'id': 7, 'children':
          [ {'id': 3, 'children': None} ]
        }
      ]
    }
  ]
  </code></pre>
  
  <p>I also have a list of selected IDs, <code>[4, 5, 6, 7]</code>.  I want to traverse the list and for each object in the list, add a <code>selected</code> key with a value of <code>1</code> if it is selected, and <code>0</code> if it is not.</p>
  
  <p>Currently I am doing this recursively with this function:</p>
  
  <pre><code>def mark_selected(tree, selected):
      for obj in tree:
          obj['selected'] = 1 if obj['id'] in selected else 0
          if obj['children'] is not None:
              obj['children'] = mark_selected(obj['children'], selected)
      return tree
  </code></pre>
  
  <p>This seems to work fine, but I was wondering if there was a more clever way to do this, possibly using list comprehension or generators.</p>
  
  <p>Can anyone come up with a more elegant solution for this?</p>