How to shorten list by grouping values that appear multiple times in Python 3.7? : <p>I have a list that's something like this:</p>

<pre><code>foo = ['bar', 'bar', 'bar', 'foo', 'foo','bar']
</code></pre>

<p>and I want it to be converted to something like this:</p>

<pre><code>'1-3 = bar'
'4-5 = foo'
'6 = bar'
</code></pre>

<p>The conversion needs to be done automatically, to accommodate list changes. How would I go about automating this?</p>
