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 remove duplicates using a set, with the keys being tuples of the first two parts:</p>

<pre><code>&gt;&gt;&gt; ips = ["192.0.0.3", "0.0.0.0", "192.0.10.24"]
&gt;&gt;&gt; seen = set()
&gt;&gt;&gt; for ip in ips:
...     key = tuple(ip.split(".")[:2])
...     if key not in seen:
...         print(ip)
...         seen.add(key)
... 
192.0.0.3
0.0.0.0
</code></pre>

<p>Or alternatively using the <a href="https://docs.python.org/3/library/ipaddress.html" rel="nofollow">ipaddress</a> module:</p>

<pre><code>&gt;&gt;&gt; from ipaddress import ip_network
&gt;&gt;&gt; ips = ["192.0.0.3", "0.0.0.0", "192.0.10.24"]
&gt;&gt;&gt; seen = set()
&gt;&gt;&gt; for ip in ips:
...     key = ip_network(ip + "/16", strict=False)
...     if key not in seen:
...         print(ip)
...         seen.add(key)
... 
192.0.0.3
0.0.0.0
</code></pre>

python arrays set