Python Check if suffix matches with any string in given list?

Checking if a suffix matches with any string in a list is a common string manipulation task in Python. A suffix is the ending part of a string, and we need to verify if any string in our list ends with the given suffix pattern.

Understanding Suffixes

A suffix is a substring that appears at the end of a string. For example, in "Sunday", the suffix "day" appears at the end. Let's see how to check if any string in a list ends with a specific suffix.

Using any() with endswith()

The most efficient approach combines any() with the endswith() method. The any() function returns True if at least one item in an iterable is True.

Example

# List of days
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]

# Suffix to check
suffix = "day"

# Check if any string ends with the suffix
if any(day.endswith(suffix) for day in days):
    print(f"Suffix '{suffix}' matches with strings in the list.")
else:
    print(f"Suffix '{suffix}' does not match any string in the list.")
Suffix 'day' matches with strings in the list.

Using filter() with endswith()

The filter() function creates an iterator of elements that satisfy a given condition. We can use it to find all strings that end with our suffix.

Example

# List of file names
files = ["document.pdf", "image.png", "script.py", "data.csv"]

# Suffix to check
suffix = ".py"

# Find all files with the suffix
matching_files = list(filter(lambda file: file.endswith(suffix), files))

if matching_files:
    print(f"Files ending with '{suffix}': {matching_files}")
else:
    print(f"No files end with '{suffix}'")
Files ending with '.py': ['script.py']

Using List Comprehension

List comprehension provides a clean and readable way to filter strings that end with a specific suffix.

Example

# List of programming languages
languages = ["Python", "JavaScript", "Java", "C++", "Ruby"]

# Suffix to check
suffix = "Script"

# Find matching languages
matches = [lang for lang in languages if lang.endswith(suffix)]

if matches:
    print(f"Languages ending with '{suffix}': {matches}")
    print(f"Count: {len(matches)}")
else:
    print(f"No languages end with '{suffix}'")
Languages ending with 'Script': ['JavaScript']
Count: 1

Comparison of Methods

Method Returns Best For
any() + endswith() Boolean (True/False) Just checking existence
filter() + endswith() Filtered items Getting matching strings
List comprehension List of matches Readable filtering

Case-Insensitive Suffix Matching

For case-insensitive matching, convert both the string and suffix to lowercase before comparison.

Example

# List of names
names = ["Alice", "BOB", "Charlie", "dave"]

# Suffix to check (case-insensitive)
suffix = "CE"

# Case-insensitive matching
matches = [name for name in names if name.lower().endswith(suffix.lower())]

if matches:
    print(f"Names ending with '{suffix}' (case-insensitive): {matches}")
else:
    print(f"No names end with '{suffix}' (case-insensitive)")
Names ending with 'CE' (case-insensitive): ['Alice']

Conclusion

Use any() with endswith() for simple boolean checks. Use list comprehension or filter() when you need the actual matching strings. Always use endswith() for proper suffix matching instead of substring searching.

Updated on: 2026-03-15T18:36:18+05:30

450 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements