Organize a list of unique integers and create ranges/summaries when possible
<p>I am trying to setup a function or a class to store unique integers with abilities to add,remove or check for existence. This of course can be easily achieved using normal set. The tricky part here is displaying of ranges. I mean, if I have all the numbers between 100 and 20000 I don't want to display a huge list all the numbers in between, but rather displaying 100-20000. </p>

<p>Consider the following example:</p>

<pre><code>numbers 3000,3008-3015,3020,3022,3030-3043,3068
</code></pre>

<p>The goal is to create a function or class to add, check or retrieve information about current numbers. Here is how I imagine this: </p>

<pre><code>&gt;&gt;&gt; f_check(3016) 
False
&gt;&gt;&gt; f_check(3039)
True
&gt;&gt;&gt; f_add(3016)
3000,3008-3016,3020,3022,3030-3043,3068
&gt;&gt;&gt; f_remove(3039)
3000,3008-3016,3020,3022,3030-3038,3040-3043,3068    
&gt;&gt;&gt; f_add(3100)
3000,3008-3016,3020,3022,3030-3038,3040-3043,3068,3100
</code></pre>

<p>and so forth... </p>

<p>Again, pay attention to the range part (3030-3038, 3040-3043) etc. I don't want to display every single entry for the consecutive numbers but rather displaying a "summary" or range. </p>

<p>Using the same example again, if I add 3021, I'll expect the following result:</p>

<pre><code>&gt;&gt;&gt; f_add(3021)
3000,3008-3016,3020-3022,3030-3043,3068
</code></pre>

<p>Many thanks in advance for your thoughts!</p>

<p>Since you did not specify what you mean by formatting, I'm assuming you mean sorting a list of numbers based upon their value in ascending order. Also, I modified your list and removed the dashes between certain numbers.</p>

<p>With that being said, what I believe you're asking for is the <code>sorted()</code> function. Straight from the documentation:</p>

<blockquote>
  <p>Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable.</p>
</blockquote>

<p>I'd just make a class however, and add the methods that you outlined above. The class would be pretty simple. Here's an example of what I'd do:</p>

<pre><code>class IntList:
  def __init__(self, *args):
    self.lst = [int(i) for i in args]
    self.lst.sort()

  def add(self, arg):
    self.lst.append(arg)
    self.lst.sort()

  def remove(self, arg):
    self.lst.remove(arg)
    self.lst.sort()

  def check(self, arg):
    return arg in self.lst
</code></pre>

<p>Here is an example usage:</p>

<pre><code>&gt;&gt;&gt; lst = IntList(1, 2, 3, 4, 5)
&gt;&gt;&gt; lst.add(6)
&gt;&gt;&gt; lst.lst
[1, 2, 3, 4, 5, 6]
&gt;&gt;&gt; lst.remove(2)
&gt;&gt;&gt; lst.lst
[1, 3, 4, 5, 6]
&gt;&gt;&gt; lst.check(4)
True
&gt;&gt;&gt; lst.check(90)
False
</code></pre>

<p>If my assumptions are wrong please specify what exactly you mean.</p>

python integer set