Trying to remove all occurances of a few patterns from a list

Question:

So I have a list, something like this:

check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']

I’m trying to remove all the elements containing any of the elements of this list:

['/30', '/31', '/32']

For some reason, the list is being partially trimmed down, and I cannot quite understand why.

My code:


y = 30

while y <=32:
    x.append('/'+str(y)) # To generate the list of elements
    y = y + 1



check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']

print(len(check))
s = []
for i in x:
    for j in check:
        if i in j:
            print("True")
            print(i)
            print(j)
            check.remove(j)

print(len(check))
print(check)

Any help is appreciated, since I’m stuck debugging this.

Instead of if i in j, I have also used if i == j[-3:], also with the same partially trimmed result.

The result, just FYI:

[‘10.92.145.17/29’, ‘10.92.145.25/29’, ‘10.202.1.113/32’, ‘10.202.1.122/32’, ‘10.202.1.126/32’, ‘10.202.1.132/32’, ‘10.202.1.136/32’, ‘10.202.1.139/32’, ‘10.202.1.141/32’, ‘10.202.1.146/32’, ‘10.202.1.149/32’, ‘10.202.1.151/32’, ‘10.202.1.154/32’, ‘10.202.1.156/32’, ‘10.202.1.158/32’, ‘10.202.1.160/32’, ‘10.202.1.162/32’, ‘10.202.1.165/32’, ‘10.202.1.168/32’, ‘10.202.1.170/32’, ‘10.202.1.172/32’, ‘10.202.1.174/32’, ‘10.202.1.176/32’, ‘10.202.1.178/32’, ‘10.202.1.180/32’, ‘10.202.1.182/32’]

Answers:

Use .endswith()

Or .split(“/“) and test whether
final component is in the 30’s.

——

Print out repr() of the string to verify there’s are no hidden, invisible, unprintable characters lurking within it, for example, a newline.
Repr stands for representation, and it will reveal such characters.

Answered By: J_H

just need to check last 3 character of string, use list slicing and check if it is present in filter keyword or not.

by overwriting check

>>> filtered = {'/30','/31','/32'}
>>> check[:] = [i for i in check if not(i[-3:] in filtered)]
>>> check
['10.92.145.17/29', '10.92.145.25/29']
Answered By: sahasrara62

Will, you have a simple logical error because you are using 2 Iterations.
assuming that the list you have contain 100 ip, in this code you actually iterate through it 300 times.
but each time you checking just one value at a time

["/30", "/31", "/32"]

also if you removed an element from the list the iteration will continue on the next element and you will miss one, so the double iteration here will not work as intended.

you can simply use all conditions at once like…

check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']
s = []

for ip in check:
    if ("/30" not in ip) and ("/31" not in ip) and ("/32" not in ip):
        s.append(ip)

print(s)

or more combined code like this one will work too

check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']
x = ["/30", "/31", "/32"]
s = [j for j in check if not any(i in j for i in x)]
print(s)
Answered By: Khaled Sayed

Try this:

x = [f"/{i}" for i in range(30, 33)] #Including 30, 31, 32

check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32',
         '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32',
         '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32',
         '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32',
         '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32',
         '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32',
         '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32',
         '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32',
         '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32',
         '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']

filtered_list = [i for i in check for j in x if j not in i]
print(filtered_list)
>>> ['10.92.145.17/29', '10.92.145.17/29', '10.92.145.25/29', '10.92.145.25/29', '10.45.33.109/32', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.181/32', '10.202.1.182/32', '10.202.1.182/32']

Usually its not a good idea to mutate a list while iterating/looping through it, also list comprehension is a bit faster than normal for loops
Hope it helps.

Answered By: Daniel Afriyie
check = ['10.92.145.17/29', '10.92.145.25/29', '10.45.33.109/32', '10.202.1.113/32', '10.202.1.119/32', '10.202.1.122/32', '10.202.1.124/32', '10.202.1.126/32', '10.202.1.130/32', '10.202.1.132/32', '10.202.1.135/32', '10.202.1.136/32', '10.202.1.137/32', '10.202.1.139/32', '10.202.1.140/32', '10.202.1.141/32', '10.202.1.145/32', '10.202.1.146/32', '10.202.1.148/32', '10.202.1.149/32', '10.202.1.150/32', '10.202.1.151/32', '10.202.1.152/32', '10.202.1.154/32', '10.202.1.155/32', '10.202.1.156/32', '10.202.1.157/32', '10.202.1.158/32', '10.202.1.159/32', '10.202.1.160/32', '10.202.1.161/32', '10.202.1.162/32', '10.202.1.164/32', '10.202.1.165/32', '10.202.1.167/32', '10.202.1.168/32', '10.202.1.169/32', '10.202.1.170/32', '10.202.1.171/32', '10.202.1.172/32', '10.202.1.173/32', '10.202.1.174/32', '10.202.1.175/32', '10.202.1.176/32', '10.202.1.177/32', '10.202.1.178/32', '10.202.1.179/32', '10.202.1.180/32', '10.202.1.181/32', '10.202.1.182/32']

newlist = []
for n,item in enumerate(check):
    if not item.split("/")[-1] in ['30', '31', '32']: 
        newlist.append(item)
print(newlist)

# result
# ['10.92.145.17/29', '10.92.145.25/29']
Answered By: PythonProgrammi
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.