title: How can I optimise this repeated sequence sort?
body: <p>I'm sorting a list of dictionaries by one of the dict values and want to have a sorted sequence like 1,2,3,1,2,3,3 This is what I've come up with:   </p>
  
  <p><strong>Input:</strong></p>
  
  <pre><code>dicts = [{'name':'a', 'order':2}, {'name':'b', 'order':2}, {'name':'c', 'order':1}, {'name':'d', 'order':3}, {'name':'e', 'order':3}, {'name':'f', 'order':1}, {'name':'g', 'order':1}]
  </code></pre>
  
  <p><strong>Function:</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from itertools import groupby, zip_longest
  from typing import List
  
  def seq_sort(input: List[dict]) -&gt; List[dict]:
    new_seq = []
    # order the dicts
    ordered = sorted(input, key=lambda x: x['order'])
    # group the dicts
    group = [
      [p for p in v] for k, v in groupby(
        ordered,
        key=lambda x:x['order']
      )
    ]
    # put into new list in sequential order
    # grouped lists could be different lengths
    for groups in zip_longest(*group):
        for g in groups:
            if g is not None:
              new_seq.append(g)
    return new_seq
  
  print(seq_sort(dicts))
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>[{'name': 'c', 'order': 1}, {'name': 'a', 'order': 2}, {'name': 'd', 'order': 3}, {'name': 'f', 'order': 1}, {'name': 'b', 'order': 2}, {'name': 'e', 'order': 3}, {'name':'g', 'order': 1}]
  </code></pre>
  
  <p>This works but I currently have brain fog and feel that this is not the best way to do it, how should I do this better?</p>