Python Forum
Compare lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Compare lists (/thread-42140.html)



Compare lists - w_i_k_i_d - May-17-2024

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: []


RE: Compare lists - menator01 - May-17-2024

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)



RE: Compare lists - w_i_k_i_d - May-17-2024

Big Grin Thank you
I noticed I was missing 'else'

[attachment=2871]

Thanks for your suggestions.


RE: Compare lists - deanhystad - May-17-2024

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))



RE: Compare lists - Pedroski55 - May-18-2024

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]



RE: Compare lists - ebn852_pan - May-23-2024

Is this possible? You are banning a local lan to be connected to a network. And how does this work out?


RE: Compare lists - deanhystad - May-23-2024

There is no banning going on. It is just a programming exercise.