Using a set would be much faster than iterating through the lists.

checklist = ['A', 'FOO']
words = ['fAr', 'near', 'A']
matches = set(checklist).intersection(set(words))
print(matches)  # {'A'}
Answer from Michelle Welcks on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › exact match using list comprehension
r/learnpython on Reddit: exact match using list comprehension
February 22, 2018 -

I have this list I am trying to find a exact match for using a list comprehension.

my_list =['abc','abc_ab']
result = [i for i in my_list if 'abc_a' in i ]
the list is finding and matching on abc_ab

I want to match abc_a not abc_ab. how do I use a list comprehension to match on the exact phrase.

wait never mind. I needed == not in.

🌐
Python Forum
python-forum.io › thread-749.html
Check to see if a string exactly matches an element in a list
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 bel...
🌐
Reddit
reddit.com › r/learnpython › match exact strings from a list in a pandas string column
r/learnpython on Reddit: match exact strings from a list in a pandas string column
July 19, 2021 -

Is there a way to match a list of strings exactly with the strings in a pandas column to filter out the ones that do not have?

Say, words = ['ab', 'ml']

df =

data
'example string ab'
'absolute value'

After filtering, I must get only the row with value 'example string ab' for it contains exact string 'ab' from the list 'words'.

🌐
Reddit
reddit.com › r/learnpython › how to use str.contains to get exact matches and not partial ones?
r/learnpython on Reddit: How to use str.contains to get exact matches and not partial ones?
November 10, 2021 -

Hi, I don't get why when I use str.contains to get exact matches from a list of keywords, the output still contains partial matches. Here is an extract of what I have (I'm only including one keyword in the list for the example):

keyword= ['SE.TER.ENRL']

subset = df[df['Code'].str.contains('|'.join(keyword), case=False, na=False)]

Output: ['SE.TER.ENRL' 'SE.TER.ENRL.FE' 'SE.TER.ENRL.FE.ZS']

Does anyone know how to get around this?

Thanks!

🌐
Note.nkmk.me
note.nkmk.me › home › python
String Comparison in Python (Exact/Partial Match, etc.) | note.nkmk.me
April 29, 2025 - Search for a string in Python (Check if a substring is included/Get a substring position) Similar to numbers, the == operator checks if two strings are equal. If they are equal, True is returned; otherwise, False is returned. print('abc' == ...
🌐
Top Mini Sites
topminisite.com › home › internet › how to match exact strings in a list using python?
How to Match Exact Strings In A List Using Python in 2025?
In Python, you can match exact strings in a list by iterating through the list and checking for equality between each element and the target string.
🌐
Quora
quora.com › In-Python-how-do-you-check-for-an-exact-string-match-while-ignoring-case-sensitivity
In Python, how do you check for an exact string match while ignoring case sensitivity? - Quora
Answer (1 of 6): A few helpful ideas. Python re is very forgiving just replace occurrences using re.sub then no need of a match before having to further process after a match. And if it finds no sub match or search it wont throw an error. If re doesn't find a sub it skips it anyways. And re.find...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 76903332 › how-to-match-exact-string-in-a-list-using-python
How to match exact string in a list using Python - Stack Overflow
You can split the string by spaces (or also possibly brackets, if you want to be able to match cat, not [cat), then use in on the resultant array. import re cmd_list = ["passwd", "/etc/passwd"] string = "[cat /etc/passwd | grep finftp]" split_string = re.split('[ \[\]]', string) matches = [] for word in sorted(cmd_list): if word in split_string: matches.append(word) print(matches)
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
1 month ago - Every example runs in a standard ... release, Python 3.14) and uses only the standard library, with no extra packages. ... Use the in operator ("target" in my_list) for a fast, easy-to-read check; it returns True or False. On a list, in checks for an exact match, not a partial one.
🌐
AskPython
askpython.com › python › examples › matching-strings-using-regular-expressions
Matching Entire Strings in Python using Regular Expressions - AskPython
February 27, 2023 - To match an exact string, you can use the () grouping operator to create a capturing group around the string, and then use a backreference to match the exact same string again. For example we have a text file given below: This is a sample text file.
🌐
Quora
quora.com › How-do-you-use-a-Python-regular-expression-to-find-an-exact-string-match
How to use a Python regular expression to find an exact string match - Quora
Answer (1 of 3): First of all, I should mention that Regular Expressions is extremely versatile. Whilst it can be used in this application, a normal search algorithm can do it fine. For this case; [code]paragraphs = re.findall(r' (.*?) ', str(respData)) # . = Any Character except fo...
Top answer
1 of 3
1

A much better idea for exact matches is to store the keywords in a set

keywords = {'foo', 'bar', 'joe', 'mauer'}
listOfStrings = ['I am frustrated', 'this task is foobar', 'mauer is awesome']

[s for s in listOfStrings if any(w in keywords for w in s.split())]

This only tests each word in listOfStrings once. Your method (or using regex) looks at every word in listOfStrings for each keyword. As the number of keywords grows, that will be very inefficient.

2 of 3
0

Instead of checking if each keyword is contained anywhere in the string, you can break the sentences down into words, and check whether each of them is a keyword. Then you won’t have problems with partial matches.

Here, RE_WORD is defined as the regular expression of a word-boundary, at least one character, and then another word boundary. You can use re.findall() to find all words in the string. re.compile() pre-compiles the regular expression so that it doesn’t have to be parsed from scratch for every line.

frozenset() is an efficient data structure that can answer the question “is the given word in the frozen set?” faster than is possible by scanning through a long list of keywords and trying every one of them.

#!/usr/bin/env python2.7

import re

RE_WORD = re.compile(r'\b[a-zA-Z]+\b')

keywords = frozenset(['foo', 'bar', 'joe', 'mauer'])
listOfStrings = ['I am frustrated', 'this task is foobar', 'mauer is awesome']

for i in listOfStrings:
    for word in RE_WORD.findall(i):
        if word in keywords:
            print i
            continue
🌐
Quora
quora.com › How-can-I-do-a-full-string-match-in-Python
How to do a full string match in Python - Quora
Answer (1 of 6): Something like below (typed in interactive Python) [code]>>> domain = "www.example.com" >>> wordlist = ['example', 'pvt','ltd','examp'] >>> [word in domain for word in wordlist] [True, False, False, True] [/code]It says if the word from wordlist appears in the string domain (and...