Answers for "python not in or in list"

5

pandas not in list

>>> df
  countries
0        US
1        UK
2   Germany
3     China
>>> countries
['UK', 'China']
>>> df.countries.isin(countries)
0    False
1     True
2    False
3     True
Name: countries, dtype: bool
>>> df[df.countries.isin(countries)]
  countries
1        UK
3     China
>>> df[~df.countries.isin(countries)]
  countries
0        US
2   Germany
Posted by: Guest on March-27-2020
1

python checking not in list

a = [1, 2, 3, 4, 5, 6]
b = 7
c = 4

# use "not in" to check if something is not an element of a list
# use "in" to check if something is an element of a list

if b not in a:
  print('True')
else:
  print('False')

if c in a:
  print('True')
else:
    print('False')
Posted by: Guest on January-30-2021

Python Answers by Framework

Browse Popular Code Answers by Language