Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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.)

>Solution :

def your_function(list_of_chars):
    return "xyz" in "".join(list_of_chars)
your_list = ['_','x','y','z','_']
your_function(your_list)  # == True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading