Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains an element from a list in Python

I have something like this:

extensionsToCheck = ['.pdf', '.doc', '.xls']  for extension in extensionsToCheck:     if extension in url_string:         print(url_string) 

I am wondering what would be the more elegant way to do this in Python (without using the for loop)? I was thinking of something like this (like from C/C++), but it didn't work:

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

Edit: I'm kinda forced to explain how this is different to the question below which is marked as potential duplicate (so it doesn't get closed I guess).

The difference is, I wanted to check if a string is part of some list of strings whereas the other question is checking whether a string from a list of strings is a substring of another string. Similar, but not quite the same and semantics matter when you're looking for an answer online IMHO. These two questions are actually looking to solve the opposite problem of one another. The solution for both turns out to be the same though.

like image 944
tkit Avatar asked Jun 30 '11 07:06

tkit


People also ask

How do you check if a string contains something from a list Python?

The in Operator It returns a Boolean (either True or False ). To check if a string contains a substring in Python using the in operator, we simply invoke it on the superstring: fullstring = "StackAbuse" substring = "tack" if substring in fullstring: print("Found!") else: print("Not found!")

How do you check if a string contains any strings from a list?

Using String.contains() method for each substring. You can terminate the loop on the first match of the substring, or create a utility function that returns true if the specified string contains any of the substrings from the specified list.

How do you check if an element exists in a list Python?

We can use the in-built python List method, count(), to check if the passed element exists in the List. If the passed element exists in the List, the count() method will show the number of times it occurs in the entire list. If it is a non-zero positive number, it means an element exists in the List.


2 Answers

Use a generator together with any, which short-circuits on the first True:

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

EDIT: I see this answer has been accepted by OP. Though my solution may be "good enough" solution to his particular problem, and is a good general way to check if any strings in a list are found in another string, keep in mind that this is all that this solution does. It does not care WHERE the string is found e.g. in the ending of the string. If this is important, as is often the case with urls, you should look to the answer of @Wladimir Palant, or you risk getting false positives.

like image 94
Lauritz V. Thaulow Avatar answered Sep 29 '22 19:09

Lauritz V. Thaulow


extensionsToCheck = ('.pdf', '.doc', '.xls')  'test.doc'.endswith(extensionsToCheck)   # returns True  'test.jpg'.endswith(extensionsToCheck)   # returns False 
like image 23
eumiro Avatar answered Sep 29 '22 18:09

eumiro