List comprehension vs. lambda + filter
List comprehension and the combination of lambda functions and the filter() function in Python are both used to filter a list and return a new list with only the elements that satisfy a certain condition.
Here is an example of a list comprehension that filters a list of numbers and returns only the even numbers:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = [x for x in numbers if x % 2 == 0]
print(even_numbers)
Output: [2, 4, 6]
Here is an equivalent example using a lambda function and the filter() function:
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))
Output: [2, 4, 6]
Both of these methods will give the same output. Which one to use depends on the readability of the code and developer preference.