Python: how to check if a list of values is contained within a range

I’m working with a list of values structured as dataframe and I’d like to compare each value of this list with another dataframe (kinda like this one below):

Name    Start    End
Blue    10       28
Red     23       25
Green   89       107
Purple  168      216
Yellow  21       40

Now let’s suppose that the list of values is something like this:

Name    Value
W       37
X       176
Y       43
Z       96

For each element in the “Value” column I’d like to check if that value is contained inside each “Start” – “End” range of the first dataframe and add it to a list (i.e. contained = []). In the example W (37) is contained in Yellow, X (176) in Purple, Z (96) in Green while Y is not matching so it will be excluded (or added to another list not_contained = []). How can I do that? Thank you all.


As a loose solution, you can iterate over a row and match the value. I have created a solution using the mentioned approach. import pandas as pd

df = pd.DataFrame({'name': ['Blue', 'Red', 'Green', 'Purple', 'Yellow'],

df2 = pd.DataFrame({'name':['W','X','Y','Z'],

not_contained = []

def checking(val):
    # iterate over df rows
    for index, row in df.iterrows():
        if val >= row['start'] and val <= row['end']:
            # if value is found, append to contained list and return
            return True
    # if value is not found, append it to not_contained list

# apply function

print("contained: ",contained)
print("not_contained: ",not_contained)


contained:  [37, 176, 96]
not_contained:  [43]