No, str.startswith()
doesn't take a range.
You'd have to parse out the first part and test it as an integer; filtering is also easier done with a list comprehension:
[ip for ip in ip_addresses if 257 <= int(ip.partition('.')[0]) <= 310]
The alternative would be to use the ipaddress
library; it'll reject any invalid address with a ipaddress.AddressValueError
exception, and since addresses that start with anything over 255 are invalid, you can easily co-opt that to filter out your invalid addresses:
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)]