Python Forum

Full Version: Check to see if a string exactly matches an element in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm splitting the path from the filename and I would like to compare the file name against a list of elements in a list and if there's an exact match to any of the elements in the list, flip a variable value to true.  This is what I have below, and it seems to be working, but I want to run this by those with more experience to see if I missed anything.


file_lst = ['.file.edn', '.config.json', 'TEMPLATE', 'README.md']
contains_file_in_file_lst = False
path, fname = os.path.split(file_information.filename)
if any(fname in filename for filename in file_lst):
   contains_file_in_file_lst = True
If you want an exact match, then it's as simple as:
if fname in file_lst: