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
🌐
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 »
🌐
GeeksforGeeks
geeksforgeeks.org › python › filter-in-python
filter() in python - GeeksforGeeks
DSA Python · Data Science · NumPy · Pandas · Practice · Django · Flask · Last Updated : 18 Mar, 2026 · filter() function is used to extract elements from an iterable (like a list, tuple or set) that satisfy a given condition.
Published   5 days ago
🌐
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
3 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.
🌐
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.
🌐
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.
🌐
Simplilearn
simplilearn.com › home › resources › software development › filter in python: an introduction to filter() function [with examples]
Filter in Python: An Introduction to Filter() Function [with Examples]
July 10, 2024 - Filter in python is used to filter the given sequence with the help of a function that tests each element in the iterable to be true or not. Learn more now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
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.
🌐
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.
🌐
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 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.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.filter.html
pandas.DataFrame.filter — pandas 3.0.1 documentation
>>> # select columns by name >>> df.filter(items=["one", "three"]) one three mouse 1 3 rabbit 4 6
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › python › python_filter_function.htm
Python filter() Function
The Python filter() function allows you to filter out elements from an iterable object based on the specified condition. An object is said to be iterable if it allows its item to be retrieved through iteration, such as lists, tuples, or strings.