title: More elegant way to avoid this hacky implementation of single line reduce for grouping a collection?
body: <p>In <a href="https://stackoverflow.com/questions/54337537/how-to-create-multiple-lists-from-one-list-according-to-matching-substrings/54337725#54337725">my evolved answer to this question</a>, I came up with a way to do a single line (well, single expression) <code>reduce</code> to create the results of <code>groupby</code> as defined by many other languages (Kotlin, ObjC, Swift, Smalltalk, at least).</p>
  
  <p>My initial attempt looked like:</p>
  
  <pre><code>def keyFunc(value):
      return derivative_of_value
  
  grouped = reduce(
      lambda accum, each: accum[keyFunc(each)].append(each),
      allValues,
      defaultdict(list))
  </code></pre>
  
  <p>As stated in my Aside/Tangent there, the problem is the lambda. A lambda is limited to a single expression. And for it to work in reduce, it must return a modified version of the accumulated argument.</p>
  
  <p>So I came up with the following hack, using a tuple to move the dict reference from reduction to reduction, but also force the (ignored) side effect of updating the same dict:</p>
  
  <pre><code>from functools import reduce
  grouped = reduce(
      lambda accum, each: (accum[0], accum[0][keyFunc(each)].append(each)),
      allValues,
      (defaultdict(list), None))[0]
  </code></pre>
  
  <p><em>The Question</em> is... is there a better way? <strong>Given</strong> the constraint that I want to try and still use a single expression reduce without a bunch of helper functions.</p>
  
  <p><em>(I recognize that sometimes the code is telling you something, but I'm interested in this case for the academic side of things)</em></p>