nodes: 0, module; 1, function_definition; 2, function_name:regroup; 3, parameters; 4, block; 5, identifier:target; 6, identifier:expression; 7, expression_statement; 8, if_statement; 9, return_statement; 10, comment:"""
      Regroups a list of alike objects by a common attribute.
  
      This complex tag is best illustrated by use of an example:  say that
      ``people`` is a list of ``Person`` objects that have ``first_name``,
      ``last_name``, and ``gender`` attributes, and you'd like to display a list
      that looks like:
  
          * Male:
              * George Bush
              * Bill Clinton
          * Female:
              * Margaret Thatcher
              * Colendeeza Rice
          * Unknown:
              * Pat Smith
  
      The following snippet of template code would accomplish this dubious task::
  
          {% regroup people by gender as grouped %}
          <ul>
          {% for group in grouped %}
              <li>{{ group.grouper }}
              <ul>
                  {% for item in group.list %}
                  <li>{{ item }}</li>
                  {% endfor %}
              </ul>
          {% endfor %}
          </ul>
  
      As you can see, ``{% regroup %}`` populates a variable with a list of
      objects with ``grouper`` and ``list`` attributes.  ``grouper`` contains the
      item that was grouped by; ``list`` contains the list of objects that share
      that ``grouper``.  In this case, ``grouper`` would be ``Male``, ``Female``
      and ``Unknown``, and ``list`` is the list of people with those genders.
  
      Note that ``{% regroup %}`` does not work when the list to be grouped is not
      sorted by the key you are grouping by!  This means that if your list of
      people was not sorted by gender, you'd need to make sure it is sorted
      before using it, i.e.::
  
          {% regroup people|dictsort:"gender" by gender as grouped %}
  
      """; 11, not_operator; 12, block; 13, list_comprehension; 14, identifier:target; 15, return_statement; 16, comment:# List of dictionaries in the format:; 17, comment:# {'grouper': 'key', 'list': [list of contents]}.; 18, dictionary; 19, for_in_clause; 20, string; 21, pair; 22, pair; 23, pattern_list; 24, call; 25, string; 26, identifier:key; 27, string; 28, call; 29, identifier:key; 30, identifier:val; 31, identifier:groupby; 32, argument_list; 33, string_content:grouper; 34, string_content:list; 35, identifier:list; 36, argument_list; 37, identifier:obj_list; 38, lambda; 39, identifier:val; 40, lambda_parameters; 41, call; 42, identifier:v; 43, default_parameter; 44, identifier:f; 45, argument_list; 46, identifier:f; 47, attribute; 48, identifier:v; 49, True; 50, identifier:expression; 51, identifier:resolve
code: def regroup(target, expression):
      """
      Regroups a list of alike objects by a common attribute.
  
      This complex tag is best illustrated by use of an example:  say that
      ``people`` is a list of ``Person`` objects that have ``first_name``,
      ``last_name``, and ``gender`` attributes, and you'd like to display a list
      that looks like:
  
          * Male:
              * George Bush
              * Bill Clinton
          * Female:
              * Margaret Thatcher
              * Colendeeza Rice
          * Unknown:
              * Pat Smith
  
      The following snippet of template code would accomplish this dubious task::
  
          {% regroup people by gender as grouped %}
          <ul>
          {% for group in grouped %}
              <li>{{ group.grouper }}
              <ul>
                  {% for item in group.list %}
                  <li>{{ item }}</li>
                  {% endfor %}
              </ul>
          {% endfor %}
          </ul>
  
      As you can see, ``{% regroup %}`` populates a variable with a list of
      objects with ``grouper`` and ``list`` attributes.  ``grouper`` contains the
      item that was grouped by; ``list`` contains the list of objects that share
      that ``grouper``.  In this case, ``grouper`` would be ``Male``, ``Female``
      and ``Unknown``, and ``list`` is the list of people with those genders.
  
      Note that ``{% regroup %}`` does not work when the list to be grouped is not
      sorted by the key you are grouping by!  This means that if your list of
      people was not sorted by gender, you'd need to make sure it is sorted
      before using it, i.e.::
  
          {% regroup people|dictsort:"gender" by gender as grouped %}
  
      """
      if not target: return ''
      return [
          # List of dictionaries in the format:
          # {'grouper': 'key', 'list': [list of contents]}.
          {'grouper': key, 'list': list(val)}
          for key, val in
          groupby(obj_list, lambda v, f=expression.resolve: f(v, True))
      ]