0

I have these strings:

final1 = 'do not do that'
final2 = 'wh ghm wh matm'

and this dictionary:

contopp = {
    'do not': "don't"
    'i am': "i'm"
}

and I would like to check if any part of the dictionary keys is in any part of the strings. I know about any(), but the problem is, I also need to know what the matching key is. Is this possible?

This would just need to return 'do not', as I don't need to know which string matches.

Kai036
  • 190
  • 1
  • 2
  • 10
  • *"I would like to check if any part of the string is in any of the keys in the dictionary"* Umm, are you sure? The "a" in "matm" exists in "i am", therefore that would be considered a match? I think what you really want to do is check whether any of the dict keys exists in any of the strings. – Aran-Fey Mar 10 '19 at 09:53
  • You're totally correct, thanks for pointing that out. I edited the question to make it more clear. – Kai036 Mar 10 '19 at 09:54
  • `next(key for key in contopp if key in final1 or key in final2)` – Aran-Fey Mar 10 '19 at 09:56

0 Answers0