title: Python - Sum by Unique Attributes in a List of Attributes
body: <p>If I have a list of objects, <code>listOfObj</code>, and each object has at least 2 attributes, an <code>obj.value</code>, which is a float, and <code>obj.name</code>, which is a string, what's the best way to get a sum of all <code>obj.value</code> by each unique <code>obj.name</code> in the list?</p>
  
  <p>It's pretty much what <code>groupby</code> and <code>sum</code> does in <code>pandas</code>.</p>
  
  <p>I can think of a crude way to do it, but I'd like to know (and I'm pretty sure there is) if there's a more elegant way to do this.</p>
  
  <p>This is the solution I have currently. First, get all unique <code>name</code> in <code>listOfObj</code>:</p>
  
  <pre><code>unique_atts = {obj.name for obj in listofObj}
  </code></pre>
  
  <p>Then, perform individual <code>sum</code> operations for each attribute, like</p>
  
  <pre><code>sum_n = sum([obj.value for obj in listOfObj if obj.name == unique_atts[n]])
  </code></pre>
  
  <p>Clearly, this is not the best way to do this. What's the optimal way to solve this problem? Thank you.</p>