Faced the problem of finding list items in another multiple list. There is a list with mac addresses:

mac = [mac1, mac2, mac3] 

Multiple list with MAC and IP:

 device = [[mac1,ip1],[mac2,ip2],[mac3,ip3]] 

I'm trying to find which item from the mac list is missing from the device list. I tried to implement:

 i = 0 for values in mac: while i <= len(device)-1: if values not in device[i][0]: print('No') i += 1 

But this, in addition to what looks very unattractive, also checks only the first element of the mac list. Please tell me how to implement this correctly? I tried through the map but did not understand how to do it.

    6 answers 6

    You just need to do a search for items from the second list in the first:

     mac = ['mac1', 'mac2', 'mac3'] device = [['mac1','ip1'],['mac2','ip2'],['mac3','ip3']] for dev in device: if dev[0] in mac: print (dev[0]+" found") 

    will be:

     mac1 found mac3 found 

    UPDATE (added clarification of the author of the question):

     mac = ['mac1', 'mac2', 'mac3'] device = [['mac1','ip1'],['mac5','ip2'],['mac3','ip3']] #1 - есть в mac, но нет в device: [x for x in mac if x not in (y[0] for y in device)] #['mac2'] #2 - есть в device, но нет в mac: [x[0] for x in device if x[0] not in mac] #['mac5'] 
    • I forgot to clarify, the list of mac is all the poppies that appeared on the network for all the time, device is a list of which is now online, and I want to check if it is in the list of mac but it is not in the device, it means it is offline. As you suggested, it turns out I find all the poppies that are currently online, If it is in the device, then this is not a new device and it is online, if it is not in the device, this is a new device. But how can I check those who are in the device but not in the mac? - Vasai

    Can be implemented as a function.

     def finder(value, _list): if value in _list : return True for val in _list: if (type(val) == list or type(val) == tuple) and finder(value, val) == True : return True return False list1 = ['mac1', 'mac2', 'mac3'] list2 = [['mac1', 'ip1'], ['mac4', 'ip2'], ['mac3', 'ip3']] for val in list1: print(finder(val, list2)) # True # False # True 
       mac = ['mac1', 'mac2', 'mac3', 'mac4'] device = [['mac1','ip1'],['mac3','ip3']] print(set(mac) - set(el[0] for el in device)) # {'mac4', 'mac2'} 

        Here is the solution of your particular case, displays Yes if the element is found in the device list and No if not found:

         mac = ['mac1', 'mac2', 'mac3', 'mac4'] device = [['mac1','ip1'],['mac2','ip2'],['mac3','ip3']] for item in mac: print("Yes" if any(item == e[0] for e in device) else "No") 

        But I would recommend to implement the second list - a dictionary, something like this:

         mac = ['mac1', 'mac2', 'mac3', 'mac4'] device = {'mac1': 'ip1', 'mac2': 'ip2', 'mac3':'ip3'} print([item for item in mac if item not in device]) 
           mac = ['mac1', 'mac2', 'mac3', 'mac4', 'mac5'] device = [['mac1','ip1'],['mac2','ip2'],['mac3','ip3']] [(i,i in [j[0] for j in device]) for i in mac] [('mac1', True), ('mac2', True), ('mac3', True), ('mac4', False), ('mac5', False)] 

            Um Use a more searchable type?

             device_index = dict(device) for value in mac: if value not in device_index: print value