Coder Perfect

In Python, how do you check if a text contains a list element?


Here’s an example of what I’m talking about:

extensionsToCheck = ['.pdf', '.doc', '.xls']

for extension in extensionsToCheck:
    if extension in url_string:

I’m wondering whether there’s a more elegant method to achieve this in Python (that doesn’t include the for loop)? Something along these lines (like in C/C++) came to mind, but it didn’t work:

if ('.pdf' or '.doc' or '.xls') in url_string:

Edit: I feel compelled to explain how this differs from the question below, which has been flagged as a possible duplicate (so it won’t be closed).

The distinction is that I wanted to see whether a string is a substring of another string, whereas the other query was about seeing if a string from a list of strings is a substring of another string. Similar, but not identical, and semantics, in my opinion, important when looking for an answer online. These two queries are attempting to solve the polar opposite of one another’s difficulty. However, the answer is the same in both cases.

Asked by tkit

Solution #1

Use a generator in conjunction with any that causes a short-circuit on the first T.

if any(ext in url_string for ext in extensionsToCheck):

EDIT: I notice the OP has approved my answer. Though my solution may be a “good enough” solution to his problem and a decent general way to see if any strings in a list are found in another string, keep in mind that this is all it does. It doesn’t matter WHERE the string is located, for example, at the conclusion of the string. If this is critical, as it often is with urls, you should consult @Wladimir Palant’s response, else you risk receiving false positives.

Answered by Lauritz V. Thaulow

Solution #2

extensionsToCheck = ('.pdf', '.doc', '.xls')

'test.doc'.endswith(extensionsToCheck)   # returns True

'test.jpg'.endswith(extensionsToCheck)   # returns False

Answered by eumiro

Solution #3

It’s preferable to properly parse the URL; this way, you’ll be able to handle http://…/file.doc?foo and http://…/foo.doc/file.exe co

from urlparse import urlparse
import os
path = urlparse(url_string).path
ext = os.path.splitext(path)[1]
if ext in extensionsToCheck:

Answered by Wladimir Palant

Solution #4

If you want a one-line solution, use list comprehensions. When the file has the extensions.doc,.pdf, or.xls, the following code provides a list containing the url string; otherwise, it returns an empty list.

print [url_string for extension in extensionsToCheck if(extension in url_string)]

NOTE: This is only useful if you want to see if it contains something, not if you want to extract the specific word that matches the extensions.

Answered by psun

Solution #5

In case anyone encounters this problem again, here is another solution:

extensionsToCheck = ['.pdf', '.doc', '.xls']
url_string = 'file.doc'
res = [ele for ele in extensionsToCheck if(ele in url_string)]
> True

Answered by Aidos

Post is based on