You're getting a syntax error because an if expression has to include else, there's no default value.

But you don't need if here. The callback function of filter() just needs to return a boolean, so just use the comparison.

filtered_lst = filter(lambda x: x.startswith('A'), fruit_lst)

Note that filter() is generally not considered pythonic, conditional list comprehensions are usually preferred.

filtered_lst = [x for x in fruit_lst if x.startswith('A')]
Answer from Barmar on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-in-python
filter() in python - GeeksforGeeks
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 1 Sep, 2025 · filter() function is used to extract elements from an iterable (like a list, tuple or set) that satisfy a given condition.
Published   September 1, 2025
🌐
W3Schools
w3schools.com › python › ref_func_filter.asp
Python filter() Function
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... ages = [5, 12, 17, 18, 24, 32] def myFunc(x): if x < 18: return False else: return True adults = filter(myFunc, ages) for x in adults: print(x) Try it Yourself »
🌐
Real Python
realpython.com › ref › builtin-functions › filter
filter() | Python’s Built-in Functions – Real Python
The built-in filter() function in Python is used to process an iterable and extract items that satisfy a given condition, as determined by a filtering function.
🌐
Python documentation
docs.python.org › 3 › library › functions.html
Built-in Functions — Python 3.14.3 documentation
2 weeks ago - Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
🌐
Mimo
mimo.org › glossary › python › filter
Python filter(): Syntax, Usage, and Examples
You can use the filter() function in Python to extract elements from a sequence that meet a certain condition. Instead of writing a full loop with if statements, the Python filter() function lets you express filtering logic in a clear and concise way.
🌐
Real Python
realpython.com › python-filter-function
Python's filter(): Extract Values From Iterables – Real Python
July 31, 2023 - Python’s filter() is a built-in function that allows you to process an iterable and extract those items that satisfy a given condition. This process is commonly known as a filtering operation.
Find elsewhere
🌐
DataCamp
datacamp.com › tutorial › python-filter
Python filter(): Keep What You Need | DataCamp
June 27, 2025 - Learn how Python filter() helps you extract elements that match a condition. Use it with custom functions, lambdas, or None for data filtering tasks.
🌐
Programiz
programiz.com › python-programming › methods › built-in › filter
Python filter()
Become a certified Python programmer. Try Programiz PRO! ... The filter() function selects elements from an iterable based on the result of a function.
🌐
Python Tips
book.pythontips.com › en › latest › map_filter.html
4. Map, Filter and Reduce — Python Tips 0.1 documentation
As the name suggests, filter creates a list of elements for which a function returns true.
🌐
Vultr Docs
docs.vultr.com › python › built-in › filter
Python filter() - Filter Collection Items | Vultr Docs
November 22, 2024 - The filter() function in Python provides a convenient way for filtering collections through a function that tests each element in the iterable to be true or false.
🌐
Python Reference
python-reference.readthedocs.io › en › latest › docs › functions › filter.html
filter — Python Reference (The Right Way) 0.1 documentation
Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.
🌐
FavTutor
favtutor.com › blogs › filter-list-python
Python filter() Function: 5 Best Methods to Filter a List
November 11, 2023 - Python provides a powerful built-in function called filter() that allows you to filter elements from a list, tuple, or any other iterable based on specific conditions. In this article, we will study various methods to filter a list in python ...
🌐
Codingem
codingem.com › home › how to filter lists in python
How to Filter a List in Python [in 3 SIMPLE Ways] - codingem.com
November 2, 2022 - To filter lists in Python, use filter(). For example, filter(lambda age: age > 20, ages) filters an ages list so that ages 20+ only are left.
🌐
IONOS
ionos.com › digital guide › websites › web development › python filter function
What is Python’s filter function and how to use it
May 26, 2025 - A condition is con­sid­ered as being met if it is neither “0” or “false”. While there are other ways to filter lists and dic­tio­nar­ies in Python, the filter() function is es­pe­cial­ly efficient. It creates an object that ref­er­ences the original iterable, using the function it’s provided and an index to filter the elements.
🌐
Educative
educative.io › answers › how-to-use-the-filter-method-in-python
How to use the filter() method in Python
The filter() function facilitates a functional approach to Python programming. It takes as an argument a function and an iterable and applies the passed function to each element of the iterable.