Check if string ends with one of the strings from a list
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Python, the cleanest way to check whether a string ends with any value from a list is usually str.endswith(). The important detail is that endswith accepts a tuple of suffixes, so you normally convert your list first and let Python handle the comparison logic directly.
The Simplest Solution
str.endswith() can take either:
- a single suffix string
- a tuple of suffix strings
So if you already have a list, convert it to a tuple.
This is the most direct and idiomatic answer in Python.
Why a Tuple and Not a List
People often try this first:
That fails because endswith() expects a string or a tuple of strings, not a list.
The fix is simple:
If the suffix list is already stable, you can just define it as a tuple from the start.
That avoids repeated conversion in tight loops.
Case Sensitivity
endswith() is case-sensitive. If you are checking filenames or user input where case should not matter, normalize both sides.
This is common for extension checks, especially on systems where filenames may vary in case.
Be careful with locale-sensitive text rules in general string processing, but for file extensions .lower() is usually fine.
Compare to a Manual Loop
You can also write the logic with any():
This is readable and flexible. It becomes useful when you need custom matching logic, such as stripping whitespace or skipping empty suffixes.
Still, if all you need is "ends with one of these literal suffixes," endswith(tuple(...)) is shorter and usually clearer.
File Extension Use Cases
A common reason for this check is file filtering.
This is a good fit for:
- import pipelines
- CLI tools
- cleanup scripts
- media scanners
If you are already using pathlib, consider whether Path.suffix is clearer than endswith().
For multi-part extensions, suffixes may be more precise than a plain string test.
Empty Lists and Edge Cases
If the suffix list is empty, the result should usually be False.
That behavior is sensible, but if an empty configuration means "accept everything" in your application, handle that explicitly.
Do not rely on accidental semantics when configuration may be empty or malformed.
Performance Notes
For small suffix lists, performance differences are negligible. If the same suffix set is reused many times, store it as a tuple once:
That avoids rebuilding a tuple on every call.
If matching rules become complex, such as wildcards or patterns, use regex or fnmatch instead of forcing everything through endswith().
Common Pitfalls
- Passing a list directly into
str.endswith()instead of a tuple. - Forgetting that matching is case-sensitive.
- Using
endswith()when the real requirement is pattern matching, not literal suffix matching. - Recomputing the tuple repeatedly in hot code paths without need.
- Treating multi-part extensions such as
.tar.gzas if they were always a single simple suffix case.
Summary
- In Python, use
text.endswith(tuple_of_suffixes)to check against multiple suffixes. - Convert lists to tuples before passing them to
endswith(). - Normalize case first if the comparison should be case-insensitive.
- Use
any()only when you need custom per-suffix logic. - For path-heavy code, consider
pathlibwhen extension semantics matter.

