Startswith with a range of number instead an unit
<pre><code>ip = a list of ips
ipf = list(filter(lambda x: x if not x.startswith(str(range(257,311))) else None, ip))
</code></pre>

<p>Is is possible to do something like this? I tested it and it doesn't work.
I'd like to delete all ips from "ip" list that start with 256. 257. 258. ecc... to 310.</p>

<p>No, <code>str.startswith()</code> doesn't take a range.</p>

<p>You'd have to parse out the first part and test it as an integer; filtering is also easier done with a list comprehension:</p>

<pre><code>[ip for ip in ip_addresses if 257 &lt;= int(ip.partition('.')[0]) &lt;= 310]
</code></pre>

<p>The alternative would be to use the <a href="https://docs.python.org/3/library/ipaddress.html" rel="nofollow"><code>ipaddress</code> library</a>; it'll reject any invalid address with a <code>ipaddress.AddressValueError</code> exception, and since addresses that start with anything over 255 are invalid, you can easily co-opt that to filter out your invalid addresses:</p>

<pre><code>import ipaddress

def valid_ip(ip):
    try:
        ipaddress.IPv4Address(ip)
    except ipaddress.AddressValueError:
        return False
    else:
        return True

[ip for ip in ip_addresses if valid_ip(ip)]
</code></pre>

python list python-3.x range startswith