check if a string ends with a value in a list

Python – Check If String Ends with any in a List

In this tutorial, we will look at how to check if a string in Python ends with any value in a specified list with the help of some examples.

How to check if a string ends with any item in a list?

check if a string ends with a value in a list

You can use a combination of the Python built-in any() function and the string endswith() function to check if a string ends with any value in a list.

The built-in any() function in Python takes an iterable as an argument and returns True if any value in the iterable is truthy (evaluates to True in a boolean context).

So, for each value in the list, check if the given string ends with that value or not using the string endswith() function and pass the resulting boolean iterable as an argument to the any() function. The following is the syntax –

# check if string ends with any in list
any(s.endswith(w) for w in ls)

It returns True if the string s ends with a value that is present in the list ls.

Examples

Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.

# create strings
s1 = "Cleveland"
s2 = "Portland"
s3 = "Michigan"
s4 = "Indianapolis"
s5 = ""

# display the strings
print(s1)
print(s2)
print(s3)
print(s4)
print(s5)

Output:

Cleveland
Portland
Michigan
Indianapolis

Here, we created five strings – s1, s2, s3, s4, and s5. The first four strings are city names in the USA and the last string s5 is an empty string.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

Let’s now check if the above strings end with a value that is present in a specified list ls or not.

We’ll a list comprehension to check if a string ends with a value in the given list or not and pass the resulting boolean list as an argument to the any() function.

# create a list of accepted ending values
ls = ["land", "polis", "ville"]

# check if string ends wtih a value in ls
print(any([s1.endswith(w) for w in ls]))
print(any([s2.endswith(w) for w in ls]))
print(any([s3.endswith(w) for w in ls]))
print(any([s4.endswith(w) for w in ls]))
print(any([s5.endswith(w) for w in ls]))

Output:

True
True
False
True
False

We get True for s1, s2 and s4 as they end with values that are present in the list ls. We get False s3 as it does not end with a value that is present in the list ls. Note that we get False for the empty string.

The any() function takes an iterable as an argument, you can also directly pass an iterator which results in an iterable to the any() function (and avoid using the list comprehension).

# create a list of accepted ending values
ls = ["land", "polis", "ville"]

# check if string ends wtih a value in ls
print(any(s1.endswith(w) for w in ls))
print(any(s2.endswith(w) for w in ls))
print(any(s3.endswith(w) for w in ls))
print(any(s4.endswith(w) for w in ls))
print(any(s5.endswith(w) for w in ls))

Output:

True
True
False
True
False

We get the same result as above.

Now, in case you want to make the above check case-insensitive (returns True if the string ends with a value in the list irrespective of the case, use the string lower() function).

# check if string ends wtih a value in ls irrespective of case
print(any(s1.lower().endswith(w.lower()) for w in ls))

Output:

True

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top