'Comparing Partial Matches in Two Lists

Use Case:

I'm trying to take two lists a, and b and search for a partial domain match. Then if a value is matched it should be excluded from the third list. For all non matches I want to grab the .id and insert them into f

Example:

For example I've got list a with a bunch of data that looks like the below. I want to take the hostname field from a and search it against list b, where we should find test.online in this example. Once we find that we should discard it. I want to append the .id to f for anything that doesn't have a match. Hopefully that makes sense.

a = [{'hostname': 'apache.test.online',
      'id': '20c073b2-245e-410b-8a29-4440d4669987']}

b = ['test.online', 'google.com']

f = []
for i in b:
  for j in a:
    if j.hostname in i.domain:
      f.append(i)
result = list(set(f))

I'm new so I've been stuck on this for about a day now. My last attempt is above. I appreciate any input! Thanks!



Solution 1:[1]

Like this?

result = [
    h['id']
        for h in a
        if not any(d in h['hostname'] for d in b)
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 md2perpe