need to compare item from first list to every other item in second list and return pass or fail for each check in new list

Question:

i have two lists

lsitA = [100,200,300]
listB = [[97,103],[103,202],[202,250]]

i’m trying to check if item in listA[x] is within a certain margin from listB[i][0] or listB[i][1]
then return false, else return true. in other words i’m trying to see if lsitA[0] meets these condition either for listB[1] or listB[2] or listB[3], then return False. number of True/False must be equal to number of sub-lists in listB

this is what i have tried so far

lsitA = [100,200,300]
listB = [[97,103],[103,202],[202,250]]

def check(listB, val):
    result = []  
    
    for x in range(len(listB)):
        if listB[x][0]<=val<=listB[x][0]+4 or listB[x][1]-4<=val<=listB[x][1]:
            return result.append(print('Fail')) 
    return result.append(print('Pass'))

for i in lsitA:
    check(listB,i)

##Expected output: [Fail,Fail,Pass]
Asked By: propotato

||

Answers:

result.append() Returns nothing, and so does print().
If you want a list containing the result, I suggest doing the followings:

def check(listB, val):
    for item in listB:
        if val in range(item[0], item[1]+1):
            return 'Fail'
    return 'Pass'

result = [check(listB, i) for i in listA]
Answered By: kelvinC
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.