0

I am wondering what is correct way or better write this script?

I have create next function and second for in loop or is better way to write down ?

#!/usr/bin/env python3

 
def find_ip_on_firewall_host_dictonary(elm_IP,elm_IP_object,hosts_obj,sources_obj,create_host):

     found_IP_obj = next((fw_host for fw_host in hosts_obj if fw_host == elm_IP),False)
     if found_IP_obj:
        sources_obj.append(found_IP_obj)
     else:
        create_host.append(elm_IP_object)

     return sources_obj, create_host


def find_ip(elm_IP,elm_IP_object,hosts_obj,sources_obj,create_host):
  
     found_IP = False
     for fw_host in hosts_obj: 
       if fw_host == elm_IP:
          sources_obj.append(fw_host)
          found_IP = True
          break
     if found_IP == False:
       create_host.append(elm_IP_object)

     return sources_obj, create_host

hosts_obj= ["10.1.1.2","10.1.1.1","10.1.1.3"]    
elm_IP = "10.1.1.1"
elm_IP_object = "10.1.1.1/32"
sources_obj= []
create_host= []

source_obj, create_host = find_ip_on_firewall_host_dictonary(elm_IP,elm_IP_object,hosts_obj,sources_obj,create_host)  

print(sources_obj)
sundrys1
  • 305
  • 1
  • 8
  • Does your solution work? What do you think is deficient? – wwii Jul 05 '22 at 21:12
  • Does [Fastest way to check if a value exists in a list](https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exists-in-a-list), or [Check if something is (not) in a list in Python](https://stackoverflow.com/questions/10406130/check-if-something-is-not-in-a-list-in-python) answer your question? – wwii Jul 05 '22 at 21:21
  • 1
    The for loop could make use of the `for-else` statement which would remove the need for the flag` found_IP`. – DarrylG Jul 05 '22 at 21:30

0 Answers0