Python: check if a dictionary item is in string, then return the match
<p>I have a database table that looks like the following:</p>

<pre><code>id    phrases                    message
1     "social media, marketing"  "This person is in marketing!"
2     "finance, fintech          "This person is in finance!"
</code></pre>

<p>I've looped through the key phrases and added them to a dictionary, like so:</p>

<pre><code>messages = self.db_access.execute("SELECT * FROM messages")
messages = self.db_access.fetchall()
print(len(messages))
if self.db_access.rowcount &gt; 0:
    has_message = True
    phrase_list = {}
    for the_rule in messages:
        print(the_rule[1])
        rule = the_rule[1].split(',')
        for phrase in rule:
            phrase = str(phrase)
            phrase_list[phrase] = str(the_rule[2])
    print(phrase_list)
    print("\n")
else:
    has_message = False
</code></pre>

<p>This then produces the following:</p>

<pre><code># phrase_list
{'social media': 'This person is in marketing!', 
 'marketing': 'This person is in marketing!', 
 'finance': 'This person is in finance!', 
 'fintech': 'This person is in finance!'}
</code></pre>

<p>So, each phrase has it's own accompanying message, which is used elsewhere.</p>

<p>Now, I can compare those dict keys against a string, like so:</p>

<pre><code>descriptions = ["I am in marketing, and it is super awesome", "I am in finance, and it is super awesome"]
</code></pre>

<p>My next step is to compare that string with the keys, and if it contains any of the keywords, then print the matching key and its value/message. This is what I have so far:</p>

<pre><code>for description in descriptions:
    print(description)
    if has_message == True:
        if any(x in description for x in phrase_list):
            # print matching keyword and message
        else:
            print("No matches, but a phrase list exists")
</code></pre>

<p>So my question is, what do I need to replace that comment with to output 1) the keyword it matched with, and 2) the message associated with that keyword?</p>