Python Forum
Check to see if a string exactly matches an element in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check to see if a string exactly matches an element in a list
#1
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
Reply
#2
If you want an exact match, then it's as simple as:
if fname in file_lst:
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  removing one list element without using its index paul18fr 7 1,022 Feb-22-2025, 07:59 PM
Last Post: DeaD_EyE
  question about changing the string value of a list element jacksfrustration 4 2,001 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  extract an element of a list into a string alexs 5 3,258 Aug-30-2024, 09:24 PM
Last Post: alexs
  element in list detection problem jacksfrustration 5 1,751 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,616 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 1,865 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 1,564 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Check if two matrix are equal and of not add the matrix to the list quest 3 1,770 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 3,026 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Find (each) element from a list in a file tester_V 3 2,154 Nov-15-2022, 08:40 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020