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 Top answer 1 of 5
288
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.
2 of 5
28
[x for x in L if 'ab' in x]
Videos
12:13
How Do You Filter A List in Python? 🐍 - YouTube
05:19
Filtering a String with Python - YouTube
18:20
Python Filter List - The Easiest Methods - YouTube
How to Filter a List of Strings in Python: Finding Your Just ...
05:54
THIS Is One Of The Fastest & Cleanest Ways To Filter Lists In Python ...
Reddit
reddit.com › r/learnpython › trying to create a function that will filter out strings in my list
r/learnpython on Reddit: Trying to create a function that will filter out strings in my list
January 31, 2023 -
Is there a way I can loop through a list and filter out the strings in the list? I have the below so far
new_list = []
def num_int(str_list):
for i in str_list:
#I would want to loop though my list and filter out the strings. Can I do an if statement based on a datatype check?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.
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)
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)
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.
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.