python if string in list code example

Example 1: python find in list

# There is several possible ways if "finding" things in lists.
'Checking if something is inside'
3 in [1, 2, 3] # => True
'Filtering a collection'
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)
'Finding the first occurrence'
next(x for x in lst if ...)
next((x for x in lst if ...), [default value])
'Finding the location of an item'
[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 anything in a list is in a string python

y = any(x in String for x in List)

Example 3: python check if list contains

# To check if a certain element is contained in a list use 'in'
bikes = ['trek', 'redline', 'giant']
'trek' in bikes
# Output:
# True

Example 4: if string is in array python

if item in my_list:
    # whatever

Example 5: if list item in string python

if any(ext in url_string for ext in extensionsToCheck):
    print(url_string)

Example 6: python check if list contains value

if value in list:
  #do stuff

#Also to check if it doesn't contain
if value not in list:
  #do stuff