Problem Formulation
Imagine a scenario where you have a list of strings and your goal is to filter out only those strings that end with a specific suffix. Here’s a minimal example with the desired input and output:
Input:
my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '.py'
Output:
filtered_strings = ['hello.py', 'test.py']
Method 1: Using a Loop with endswith()
Python’s endswith()
string method can be used within a for-loop to test if each element in the list ends with the provided suffix. If the condition is met, the element can be added to a new list.
Here’s an example:
my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '.py' filtered_strings = [] for string in my_strings: if string.endswith(suffix): filtered_strings.append(string) print(filtered_strings)
In this example, filtered_strings
will only contain the elements from my_strings
that have the .py
suffix.
Method 2: Using filter() with a Lambda Function
The filter()
function in conjunction with a lambda function can provide a more concise way to accomplish 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
.
Here’s an example:
my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '.py' filtered_strings = list(filter(lambda x: x.endswith(suffix), my_strings)) print(filtered_strings)
Here, filter()
is used with a lambda function that returns True
for items ending with .py
. The result is then cast back to a list.
Method 3: List Comprehension
List comprehensions provide a more Pythonic and readable approach to creating lists based on existing lists. It’s a concise way to apply an if
condition to filter items.
Here’s an example:
my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '.py' filtered_strings = [s for s in my_strings if s.endswith(suffix)] print(filtered_strings)
The list comprehension checks each string’s suffix and includes it in the new list if the check passes.
Method 4: Using filter() with the str.endswith() Method Directly
Instead of a lambda function, you can use the endswith()
method directly in the filter()
function to achieve the same result.
Here’s an example:
my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '.py' filtered_strings = list(filter(str.endswith(suffix), my_strings)) print(filtered_strings)
Here, str.endswith(suffix)
is treated as a function that filter()
can apply to each item in the list my_strings
.
Method 5: Using Regular Expressions with re
For more complex string ending patterns, regular expressions can be used. The re
module provides a powerful way to filter strings based on patterns.
Here’s an example:
import re my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix = '\.py$' # $ denotes the end of the string filtered_strings = [s for s in my_strings if re.search(suffix, s)] print(filtered_strings)

This method compiles a regular expression pattern and checks if any strings in the list match that pattern, specifically looking for strings that end with .py
.
Bonus One-Liner Method 6: Using fnmatch.filter()
When working with files and patterns, you can use the fnmatch
module. Its filter()
function matches strings using Unix shell-style wildcards.
Here’s an example:
import fnmatch my_strings = ['hello.py', 'readme.md', 'test.py', 'documentation.txt'] suffix_pattern = '*.py' # Unix shell-style wildcard filtered_strings = fnmatch.filter(my_strings, suffix_pattern) print(filtered_strings)
The fnmatch
library filters the list based on the pattern *.py
, returning only strings that end with .py
.
Summary/Discussion
To sum up, we’ve explored six different methods to filter a list of strings in Python that end with a specific suffix:
- Using a loop with
endswith()
- Using
filter()
with a lambda function - Using list comprehension
- Using
filter()
withstr.endswith()
directly - Using regular expressions with
re
- Using
fnmatch.filter()
For everyday use, list comprehensions and filter()
are often the most straightforward. However, regular expressions are useful for more complex patterns, and fnmatch
is handy when working with filename patterns.