3

I have a list of strings containing IP addresses and DNS names with which I would like to remove the values beginning with "10." only. The sample data is as follows:

['www.example.com','1.2.3.4','4.3.2.1','example.net','10.1.1.10','10.1.1.11',...]

I thought this would be simple and started with the following:

for v in address:   
    test = re.match('(^\d+\.)',v)
    if test:
        if test.group(1) == '10.':
            address.remove(v)

The "10." addresses were not removed although I didn't receive any errors (and did some t-shooting with "print address.remove(v)" which resulted in "None" for each "10." address. Leads me to believe the regex is wrong but it seems to work other than in this capacity.

So I poked around with re.purge() - this didn't help either but don't think it's a factor in my problem. I also tried using del address[...] to no avail.

Where have I gone wrong?

Thanks very much for your attention.

2

4 Answers 4

14

The easy way would be to use list comprehensions:

filtered = [ v for v in address if not v.startswith('10.') ]
Sign up to request clarification or add additional context in comments.

2 Comments

This works although as a couple people kindly pointed out the root problem is modifying while iterating. Iterating a slice, modifying the original is the way to go - this approach solved my problem. Refer to link.
@BitBucket: N.B: The linked solution does, in fact, create a copy of the list and iterates over that. The list comprehension is surely more efficient (not to mention easier to grok).
3

One way is to create a new list using a list comprehension and str.startswith():

>>> [a for a in address if not a.startswith('10.')]
['www.example.com', '1.2.3.4', '4.3.2.1', 'example.net', '...']

This avoids using regular expressions and removing items during iteration, but does create a copy.

Comments

1

What you've done wrong here is iterating over a list while you're changing the list. That means the iteration gets confused.

See Removing Item From List - during iteration - what's wrong with this idiom? for some suggestions on how to do this correctly.

Comments

1

If would probably make sense to test first that there is really an IP address in question.

Otherwise 10.some-cdn.some-mighty-corp.com will be filtered out.

Related post

1 Comment

Certainly, and this will be accounted for once the "guts" are working. Knowing my data source, the possibility of this occurring is highly unlikely. Thanks for the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.