How can I write a lambda to check for a particular word in a list?

Question

How can I write a lambda to check for a particular word in a list?

Answer

If we’re given some list of strings, like languages = ["HTML", "JavaScript", "Python", "Ruby"], we’d write a lambda where each value checked is each item in the list.
From the example below, we see x being used as each number in a range() of numbers.

my_list = range(16)
filter(lambda x: x % 3 == 0, my_list)

What we want to do here is very similar, in that our x, or whatever you want to call it, will become each item in a list. The only difference is that our x is going to be each word in the list. And instead of checking if x is evenly divisible by 3, we need to check if it’s equivalent to ”Python”.

While a lambda can do this job very easily, it may be more tool than is needed in the particular situation.

print ("Python" in languages)        # True
print (languages.index('Python'))    # 2

Where the tool may be more effective is if we were looking for all the words that are not ‘Python’, or that come before/after it in the alphabet, etc.

print (list(filter(lambda x: x != 'Python', languages)))
# ['HTML', 'JavaScript', 'Ruby']

However, this again may be more than we need since a list comprehension can do the same thing…

print ([x for x in languages if x != 'Python'])
# ['HTML', 'JavaScript', 'Ruby']

That’s the beauty of the language… More than one way to cook an egg.

19 Likes

Why do you need to put ‘list’ in front of the filter function in the second example?

In Python 3 the filter function returns a filter object, unlike Python 2, which returned a list. Same applies to range, map, zip and possibly others that escape me, at present.

To access the object elements, we need to cast it as a list.

4 Likes

What’s the difference between a filter object and just a list? I just tried the function without the ‘list’ in front and it still seems to work

Apologies, I have the curiosity of a 6 year old lol

I’m assuming you used it in a for loop? We can iterate over a filter object, but we cannot inspect it without using list, unless you are running Python 2.

Consider…

>>> a = filter(lambda x: x % 2, [1,2,3,4,5,6,7,8,9])
>>> a
<filter object at 0x02E68A70>
>>> for x in a:
	print (x)

	
1
3
5
7
9
>>> 

I haven’t used any for loops. Below are the two codes which yield the exact same result:

print filter(lambda x: x != 'Python', languages)
print (list(filter(lambda x: x != 'Python', languages)))

I understand how the list tool works, but I don’t know why it’s necessary in this scenario, as oppose to using the first line.

That line is Python 2 code that will not work in Python 3.

>>> languages = ['HTML', 'JavaScript', 'Ruby', 'Python']
>>> print filter(lambda x: x != 'Python', languages)
SyntaxError: invalid syntax
>>> print (filter(lambda x: x != 'Python', languages))
<filter object at 0x02E48F10>
>>> print (list(filter(lambda x: x != 'Python', languages)))
['HTML', 'JavaScript', 'Ruby']
>>> 
3 Likes

Worked for me this way

print filter(lambda x: x in languages[2:-1], languages )

1 Like

I tried this and it worked for me;
print filter(lambda ele: ele is ‘Python’, languages). However, I liked your method.