I'm trying to check if a string is in one of the keys of a dictionary.
if message in a_dict: # this will only work if message is exactly one of the keys
#do stuff
I want the condition to return True even if message is only part of one of a_dict's keys.
How would I do that? Is there a way to add a regex .* in the string? Or is there a better way?
You can use any:
elif any(message in key for key in a_dict):
do_something()
If you need also the key which contains the message:
else:
key = next((message in key for key in a_dict), None)
if key is not None:
do_something(key)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With