Remove duplicates of IP addresses by netmask
<p>I have got an Array: </p>

<pre><code>[192.0.0.3, 0.0.0.0 , 192.0.10.24, ...]
</code></pre>

<p>With IP addresses and i want to remove duplicates for the /16 netmasks, so i got 192.0.0.3 but 192.0.10.24 will be removed (i don't mind which one of them, it would also be okay if the first one is removed). </p>

<p>My first thoughts were to use a regex to cast the netmask and remove every IP address which matches the then generated patttern. </p>

<p>Is there an easier way? </p>

<p>You could use a dictionary:</p>

<pre><code>&gt;&gt;&gt; res = {}
&gt;&gt;&gt; for ip in ["192.0.0.3", "0.0.0.0", "192.0.10.24"]:
...    res[tuple(ip.split('.',2)[0:2])]=ip
&gt;&gt;&gt; res.values()
['0.0.0.0', '192.0.10.24']
</code></pre>

<p>If you need the first occurence rather than the last one, a quick and dirty solution is to reverse the list first:</p>

<pre><code>&gt;&gt;&gt; res = {}
&gt;&gt;&gt; for ip in reversed(["192.0.0.3", "0.0.0.0", "192.0.10.24"]):
...    res[tuple(ip.split('.',2)[0:2])]=ip
&gt;&gt;&gt; res.values()
['0.0.0.0', '192.0.0.3']
</code></pre>

<p>Example with <code>ipaddress</code> as @eugne s suggests:</p>

<pre><code>&gt;&gt;&gt; import ipaddress
&gt;&gt;&gt; res = {}
&gt;&gt;&gt; for ip in [u"192.0.0.3", u"0.0.0.0", u"192.0.10.24"]:
...    res[ipaddress.ip_network(ip + "/16", strict=False)]=ip
&gt;&gt;&gt; res.values()
[u'192.0.10.24', u'0.0.0.0']
</code></pre>

python arrays set