0

I am trying to figure out how I can return true if my list contains any items not in my blacklist. It might sound strange but I only want to return false if the list is made up entirely of item/s in my blacklist.

Here is what I mean...

blacklist = [one, two, three]

Here is what I would like to happen to the following...

one two three four = true because four is not in the blacklist
one = false because one is in the blacklist
one two three = false because all are in the blacklist
five = true because five is not in the blacklist

Hope that makes sense.

Chris
  • 985
  • 2
  • 7
  • 17
  • you said you were trying, what did you try? – Keith Nicholas Sep 23 '20 at 01:09
  • Not homework. I haven't tried anything, I'm not sure how I can do anything like this. What I would like if for my if statement to return true if my list has anything NOT in my blacklist. – Chris Sep 23 '20 at 01:10
  • 1
    Convert to sets and check if the list is a subset of the blacklist? Simply `not (set(my_list) <= set(blacklist))` – John Coleman Sep 23 '20 at 01:12
  • This, is the problem I'm trying to figure out. No idea where to start. Googling has only returned examples of how to determine if a list contains another list. Maybe my wording is bad. – Chris Sep 23 '20 at 01:13
  • 2
    You know, it goes *a long way* if you actually provide example data that is executable code. People are much more inclined to actually try something out – juanpa.arrivillaga Sep 23 '20 at 01:18
  • Does this answer your question? [Python find elements in one list that are not in the other](https://stackoverflow.com/questions/41125909/python-find-elements-in-one-list-that-are-not-in-the-other) – ClimateUnboxed Sep 25 '20 at 13:25
  • This is a duplicate of https://stackoverflow.com/questions/41125909/python-find-elements-in-one-list-that-are-not-in-the-other and also https://stackoverflow.com/questions/2104305/finding-elements-not-in-a-list – ClimateUnboxed Sep 25 '20 at 13:25

4 Answers4

5

You can find the difference between the 2 lists by subtracting their set():

allowed = list(set(yourlist)-set(blacklist))

This will return a list to see the difference between your list and the blacklist, hence, you can use condition to see if the list is empty or not to return false or true.

Rozxjie
  • 103
  • 7
1

Try something like:

def not_in_blacklist(*args):

    blacklist = ['one', 'two', 'three']

    return any(arg not in blacklist for arg in args)

Results:

print(not_in_blacklist('one', 'two', 'three', 'four')) -> True
print(not_in_blacklist('one'))                         -> False
print(not_in_blacklist('one', 'two', 'three'))         -> False
print(not_in_blacklist('five'))                        -> True
Timus
  • 10,974
  • 5
  • 14
  • 28
0

Try something like this:

blacklist = ['one', 'two', 'three'] # these are strings, keep that in mind
lst = [1, 'two', 'three']
new_lst = []
for x in blacklist:
   s = lambda x: True if x not in lst else False
   new_lst.append(s(x))
# Check for overall difference and if it even exists
if any(new_lst):
   print(True)
else:
   print(False)

This returns the single result that you wanted. Either the list has any different items (True) or it has all items same (False)

Maxim
  • 276
  • 2
  • 6
0
testList = ['one']
blackList = ['one', 'two', 'three']
result = False
for t in testList:
    if (t not in blackList):
        result = True
        break
    else:
        continue

print(result)

Somethings like this?

IamsagarTu
  • 88
  • 1
  • 9