'Python long array of IP subnets (works, but slow)

I have a long array (+/- 1000 entries, 8 million IPs) of IP subnets, and I want to check if (a list of) certain IPs are in that array. The code that I have for it, works. But is quite "slow". Since I have to lookup more than one IP address, I want to make the search faster. Are there any ways to improve the search trough the array?

Example array:

nets = [
    '192.168.1.0/24',
    '192.168.2.0/24',
    '192.168.3.0/24',
]

The code to search:

def search(ip_address):
    for net in nets:
        if ipaddress.ip_address(ip_address) in ipaddress.ip_network(net):
            return True
    return False


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source