title: Calculating the mode in a multimodal list in Python
body: <p>I'm trying to calculate the mode (most frequent value) of a list of values in Python.  I came up with a solution, which gave out the wrong answer anyway, but I then realised that my data may be mutlimodal;  </p>
  
  <pre><code>ie 1,1,2,3,4,4 mode = 1 &amp; 4
  </code></pre>
  
  <p>Here is what I came up with so far:</p>
  
  <pre><code>def mode(valueList):
    frequencies = {}
    for value in valueList:
      if value in frequencies:
        frequencies[value] += 1
      else:
        frequencies[value] = 1
    mode = max(frequencies.itervalues())
    return mode
  </code></pre>
  
  <p>I think the problem here is that I'm outputting the value rather than the pointer of the maximum value.  Anyway can anyone suggest a better way of doing this that could work where there is more than one mode?  Or failing that how I can fix what I've got so far and identify a single mode?</p>
  
  <p>As you can probably tell I'm very new to python, thanks for the help.</p>
  
  <p>edit: should have mentioned I'm in Python 2.4</p>