Python Forum
search for more than one word using lambda - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: search for more than one word using lambda (/thread-30914.html)



search for more than one word using lambda - illmattic - Nov-12-2020

Hello,

I have this code:
sga = list(filter(lambda x: 'costs' in x, income.index))
trying to grab all index labels with the word 'costs' in it but I want to add another word to include in this grab. So for example, grab all labels that have either 'costs' or 'administrative'

I can't seem to figure it out or find it anywhere.

Any help would be appreciated.

Matt


RE: search for more than one word using lambda - bowlofred - Nov-12-2020

words = ['costs', 'administrative']
sga = [index for index in income.index if any(w in index for w in words)]



RE: search for more than one word using lambda - illmattic - Nov-13-2020

(Nov-12-2020, 08:42 PM)bowlofred Wrote:
words = ['costs', 'administrative']
sga = [index for index in income.index if any(w in index for w in words)]

thanks! this did the trick.