You can use a lambda function with the filter() function in Python to filter elements from a sequence based on a condition. The filter() function takes two arguments: a function that returns a Boolean value, and a sequence to filter. It returns a new sequence containing only the elements of the original sequence for which the function returned True. Here is an example:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
In this example, we have a list of numbers from 1 to 6. We want to create a new list containing only the even numbers from this list. We use the filter() function along with a lambda function to accomplish this.
The lambda function takes one argument x, which represents an element from the numbers list. The lambda function returns True if x % 2 == 0 (i.e., if x is even) and False otherwise. The filter() function applies this lambda function to each element of the numbers list, and returns a new list containing only the elements for which the lambda function returned True.
We assign this new list to the variable even_numbers, and then print it to the console using the print() function. The output of this code will be [2, 4, 6], which is the list of even numbers from the original list.