This simple filtering can be achieved in many ways with Python. The best approach is to use "list comprehensions" as follows:

>>> lst = ['a', 'ab', 'abc', 'bac']
>>> [k for k in lst if 'ab' in k]
['ab', 'abc']

Another way is to use the filter function. In Python 2:

>>> filter(lambda k: 'ab' in k, lst)
['ab', 'abc']

In Python 3, it returns an iterator instead of a list, but you can cast it:

>>> list(filter(lambda k: 'ab' in k, lst))
['ab', 'abc']

Though it's better practice to use a comprehension.

Answer from Eli Bendersky on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-filter-list-of-strings-based-on-the-substring-list
Python - Filter list of strings based on the substring list - GeeksforGeeks
July 11, 2025 - The lambda function uses any to check if a string contains any of the substrings from subs. The filter function returns an iterator, which we convert into a list. We can use the re module to solve this problem.
🌐
KDnuggets
kdnuggets.com › 2022 › 11 › 5-ways-filtering-python-lists.html
5 Ways of Filtering Python Lists - KDnuggets
November 14, 2022 - Learn list comprehension with code ... in scores if s >= 150] print(filtered_scores) ... To filter the string list, we will use `re.match()`. It requires the string pattern and the string....
🌐
Mimo
mimo.org › glossary › python › filter
Python filter(): Syntax, Usage, and Examples
It's particularly useful when working with various data structures in Python. The built-in filter() function creates an iterator from an iterable (like a list) by keeping only the items for which a given function returns True. It provides a clean, functional way to select a subset of data.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
June 16, 2026 - As a rule of thumb, use the list comprehension in most cases because it is easier to read. filter() can use slightly less memory for very large inputs because it processes items only as needed, and it reads cleanly when you already have a named function to pass instead of a lambda. When you need to know how many times a string appears rather than where it is, the list count() method returns the number of matches.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-filter-a-list-based-on-the-given-list-of-strings
Filter a list based on the Given List of Strings - Python - GeeksforGeeks
July 11, 2025 - Explanation: list comprehension iterates through b and keeps only those elements that do not contain any word from a_set and any() checks if any word from a_set is present in an element of b. If a match is found, the element is excluded, otherwise it is included in res. ... This method creates a single regex pattern from the filter words and scans the main list in one pass.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › filter elements from python list
Filter Elements from Python List - Spark By {Examples}
May 31, 2024 - The filter() function in Python is used to filter the elements from the list. Let's discuss this filter() method by providing different parameters like
🌐
HowToDoInJava
howtodoinjava.com › home › python › python list filter() with examples
Python List filter() with Examples
April 3, 2024 - In this example, we filter all strings containing the substring ‘an‘. # List of strings strings = ["apple", "banana", "orange", "grape", "kiwi"] # Pattern to filter pattern = "an" # Use filter() with a lambda function to filter strings containing the pattern filtered_strings = filter(lambda x: pattern in x, strings) # Convert the filtered result to a list filtered_strings_list = list(filtered_strings) print(filtered_strings_list)
🌐
Finxter
blog.finxter.com › home › learn python blog › python filter list of strings endswith()
Python Filter List of Strings endswith() - Be on the Right Side of Change
February 5, 2024 - The filter() function in conjunction ... the filtering. The filter() function applies a function to every item of the iterable (list) and returns an iterator that gets the items for which the function returns True....
🌐
W3Schools
w3schools.com › python › ref_func_filter.asp
Python filter() Function
Remove List Duplicates Reverse a String Add Two Numbers · Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp 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 »
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-filter-list-elements-starting-with-a-given-prefix-using-python
How to Filter list elements starting with a given Prefix using Python?
text = "Python programming" print(text.startswith("Py")) # True print(text.startswith("Java")) # False ... numbers = [1, 2, 3, 4, 5] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) ... def filter_by_prefix(items, prefix): return [item for item in items if item.startswith(prefix)] # Filter names starting with "A" names = ["Alice", "Bob", "Andrew", "Charlie", "Anna"] filtered = filter_by_prefix(names, "A") print("Names starting with 'A':", filtered)
🌐
Dynamo
forum.dynamobim.com › lists-logic
Filter List of Strings with Another List of String - Lists-Logic - Dynamo
December 1, 2022 - Hello, Can’t figure out how to filter one list of strings using another list of strings. I’m thinking I need to adjust the levels perhaps, or I should use another node instead of string.contains? See image below to see an example of the two lists.
🌐
w3resource
w3resource.com › python-exercises › filter › python-filter-exercise-5.php
Python function to filter out empty strings from a list
July 12, 2025 - Learn how to write a Python function that filters out all empty strings from a list of strings using the filter function. Example code provided.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python remove empty strings from list
Python Remove Empty Strings from List - Spark By {Examples}
May 21, 2024 - How to remove empty strings from a list in Python? You can use various methods of Python to remove empty strings from a list. Removing empty strings from
🌐
Vultr Docs
docs.vultr.com › python › built-in › filter
Python filter() - Filter Collection Items | Vultr Docs
November 22, 2024 - python Copy · numbers = [1, 2, 3, 4, 5, 6] even_numbers = filter(lambda x: x % 2 == 0, numbers) print(list(even_numbers)) Explain Code · The lambda function lambda x: x % 2 == 0 acts as a quick inline function that checks for even numbers, simplifying the code without needing a defined function like is_even(). Create a tuple containing various types of elements. Develop a function that identifies the desired type, such as strings.
🌐
LabEx
labex.io › tutorials › python-how-to-filter-list-with-conditions-419442
Python - How to filter list with conditions
Learn powerful Python list filtering techniques using comprehensions, filter() function, and lambda expressions to efficiently process and select list elements based on specific conditions.
🌐
Analytics Vidhya
analyticsvidhya.com › home › how to filter lists in python?
Filter Lists in Python: Mastering the Art of Data Manipulation
April 15, 2024 - Explore advanced filtering methods, including chaining filters, negating conditions, nested list filtering, regular expressions, and custom functions, to elevate your Python data filtering expertise. Want to become a full-stack data scientist? It is time for you to power ahead in your AI & ML career with our BlackBelt Plus Program! ... List filtering refers to selecting specific elements from a list based on certain conditions or criteria. It allows us to extract the desired data and discard the rest, enabling us to work with a subset of the original list.
🌐
Code With Pere
pere.hashnode.dev › python-tips-how-to-filter-numbers-and-letters-from-a-string
Python Tips: How to Filter Numbers and Letters from a String
December 29, 2022 - Numbers in string = 1234 Letters in string = python · Another option for separating numbers and letters from a string is to use the filter() function. This function takes a function and an iterable as input and returns an iterator that only ...