check string in list of strings python code example
Example 1: python find in list
# There is several possible ways if "finding" things in lists.
3 in [1, 2, 3] # => True
matches = [x for x in lst if fulfills_some_condition(x)]
matches = filter(fulfills_some_condition, lst)
matches = (x for x in lst if x > 6)
next(x for x in lst if ...)
next((x for x in lst if ...), [default value])
[1,2,3].index(2) # => 1
[1,2,3,2].index(2) # => 1
[1,2,3].index(4) # => ValueError
[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
Example 2: check if list contains string python
some_list = [
if any("abc" in s for s in some_list):
# whatever