Hello,
I am using colab.research.google.com as an online IDE
The code should keep "72.311.312.90" in ips_logged, remove the rest and update.
# Chk if ips_logged has any allowed ips, if yes keep it, remove the rest & update.
ips_logged = ["192.143.234.52", "72.311.312.90", "10.110.12.132"]
allowed_ips = [ "192.32.322.12", "72.311.312.90", , "192.168.12.23"]
for i in allowed_ips:
for x in ips_logged:
if x == i:
print("This ip: ", i, "__is Allowed")
elif x != i:
ips_logged.remove(x)
print("Not allowed ips removed", x, "___List updated: ", ips_logged)
RESULT:
Not allowed ips removed 192.143.234.52 ___List updated: ['72.311.312.90', '10.110.12.132']
Not allowed ips removed 10.110.12.132 ___List updated: ['72.311.312.90']
Not allowed ips removed 72.311.312.90 ___List updated: []
You could do something like this
ips_logged = ["192.143.234.52", "72.311.312.90", "10.110.12.132"]
allowed_ips = [ "192.32.322.12", "72.311.312.90", "192.168.12.23"]
tmp = []
for ip in allowed_ips:
if ip in ips_logged:
tmp.append(ip)
ips_logged = tmp
print(ips_logged)
one more way
ips_logged = ["192.143.234.52", "72.311.312.90", "10.110.12.132"]
allowed_ips = [ "192.32.322.12", "72.311.312.90", "192.168.12.23"]
tmp = ips_logged.copy()
for index, ip in enumerate(tmp):
if ip in allowed_ips:
print(f'Allowed: {ip}')
else:
print(f'Not Allowed: {ip}')
ips_logged.remove(ip)

Thank you
I noticed I was missing 'else'
[
attachment=2871]
Thanks for your suggestions.
You should never remove items from a list while iterating through it. This can cause all kinds of problems as you've seen. I suggest not removing items at all. It is almost always better to create a new list that only contains the items you want to keep.
In this example I use a list comprehension to build new lists.
ips_logged = ["192.143.234.52", "72.311.312.90", "10.110.12.132"]
allowed_ips = ["192.32.322.12", "72.311.312.90", "192.168.12.23"]
bad_addresses = [x for x in ips_logged if x not in allowed_ips]
ips_logged = [x for x in ips_logged if x in allowed_ips]
if len(bad_addresses) == 0:
print("All logged addresses are allowed.")
else:
print("Logged addresses that are not allowed:", ", ".join(bad_addresses))
if len(ips_logged) == 0:
print("No logged addresses were allowed.")
else:
print("Allowed logged addresses:", ", ".join(ips_logged))
In this example I do the same thing, but using sets.
ips_logged = ["192.143.234.52", "72.311.312.90", "10.110.12.132"]
allowed_ips = ["192.32.322.12", "72.311.312.90", "192.168.12.23"]
bad_addresses = list(set(ips_logged) - set(allowed_ips))
ips_logged = list(set(ips_logged) & set(allowed_ips))
if len(bad_addresses) == 0:
print("All logged addresses are allowed.")
else:
print("Logged addresses that are not allowed:", ", ".join(bad_addresses))
if len(ips_logged) == 0:
print("No logged addresses were allowed.")
else:
print("Allowed logged addresses:", ", ".join(ips_logged))
I think it is best to make a third list as menator01 did. Keep the old ips_logged list unchanged, for possible future use.
It is definitely not a good idea to delete items from a list as you iterate over that list, as deanhystad said!
As an example:
from string import ascii_lowercase
allowed = [a for a in ascii_lowercase[0:7]
actual = [a for a in ascii_lowercase]
keep = [a for a in actual if a in allowed]
Is this possible? You are banning a local lan to be connected to a network. And how does this work out?
There is no banning going on. It is just a programming exercise.