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
  element in list detection problem jacksfrustration 5 320 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 403 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  [solved] list content check paul18fr 6 683 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 696 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,521 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  Find (each) element from a list in a file tester_V 3 1,205 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,833 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,209 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,224 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How to find the second lowest element in the list? Anonymous 3 1,998 May-31-2022, 01:58 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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