title: Group list of tuples by first element, get average of second element
body: <p>I have a list of tuples, like so:</p>
  
  <pre><code>val list = List((1, 20), (2, 20), (1, 30), (2, 40), (2, 60))
  </code></pre>
  
  <p>And I want to map it to a list of tuples where:  </p>
  
  <blockquote>
    <p>The first element is unique.<br>
    The second element is averaged from tuples with identical first value.</p>
  </blockquote>
  
  <p>i.e. for the list above, a mapping to: <code>List((1, 25), (2, 40))</code></p>
  
  <p>I suppose I could do this like so:  </p>
  
  <pre><code>list.groupBy(_._1)
    .map { case (key, value) =&gt;
      (key, value.map(_._2).sum / value.length)}.toList
  </code></pre>
  
  <p>But converting to and from a map like this seems needlessly verbose and expensive. Is there a better way to do it?</p>