Home > Net >  best way to check if a list does or does not contain a series of values in the same order? (python)
best way to check if a list does or does not contain a series of values in the same order? (python)

Time:09-21

I need a function that takes a list and checks if ['x','y','z'] is in/not in that list in the exact same order.

For example:

list = ['_','x','y','z','_']
if ['x','y','z'] """not""" in list: # `not in` is the actual format of function but commented out to make example easier to explain
    return True
else:
    return False

I want:

  • If the list is [,'_','x','y','z','_'], then return True
  • If the list is [,'_','x','y','a','_'], then return False
  • If the list is [,'_','x','z','y','_'] then return False

etc.

(Note: the not is commented out because the actual function should return True if ['x','y','z'] not in list: but it is easier to explain without the not condition.)

CodePudding user response:

if you merge your list into a string using the .join() function you could then simply write:

if 'xyz' not in string:
 return true

CodePudding user response:

your_list = ['_','x','y','z','_']
"xyz" in "".join(your_list)  # = True
 
  • Related