Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter Lambda Function

How do I filter out the letter in the set of words with lambda function?

For example:

I have to filter out every word containing letter "K".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

["rabbit", "chuck", "Joe", "war", "rock", "docker"]
["chuck", "rock", "docker"]

I was told to provide the efforts:

list = ["rabbit", "chuck", "Joe", "war", "rock", "docker"]
print(list)
listfilter = list(filter(lambda x: (x == 'k'), lst))
print(listfilter)

>Solution :

There are two issues with your code:

  1. The list variable name shadows the list() builtin — pick a different name for your original list instead.
  2. Your lambda function isn’t correct. Instead of lambda x: x == k, it should be lambda x: 'k' not in x.
data = ["rabbit", "chuck", "Joe", "war", "rock", "docker"]
listfilter = list(filter(lambda x: ('k' not in x), data))

# Prints ['rabbit', 'Joe', 'war']
print(listfilter)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading