Is there a simple way to convert a list of integers to a formatted string in Python? : <p>I am writing a program which generates a list of integers, for example:</p>

<pre><code>[2, 5, 6, 7, 8, 9, 10, 15, 18, 19, 20]
</code></pre>

<p>This list can be very long, but it contains many consecutive values, as seen in this example. </p>

<p>In order to store it (in a database), I would like to convert it to a formatted string, in the following format:</p>

<pre><code>"2,5-10,15,18-20"
</code></pre>

<p>I already have in mind a solution, which is iterating through the whole list to detect consecutive values and building the string.</p>

<p>My question is: is there another, simpler way of doing this conversion in Python?</p>
