To check for the presence of 'abc' in any string in the list:
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in xs):
...
To get all the items containing 'abc':
matching = [s for s in xs if "abc" in s]
Answer from Sven Marnach on Stack OverflowTo check for the presence of 'abc' in any string in the list:
xs = ['abc-123', 'def-456', 'ghi-789', 'abc-456']
if any("abc" in s for s in xs):
...
To get all the items containing 'abc':
matching = [s for s in xs if "abc" in s]
Just throwing this out there: if you happen to need to match against more than one string, for example abc and def, you can combine two comprehensions as follows:
matchers = ['abc','def']
matching = [s for s in my_list if any(xs in s for xs in matchers)]
Output:
['abc-123', 'def-456', 'abc-456']
Help!: Check if a Python list item contains a string inside another string but with conditions
How to check if a string contains an element from a list in Python - Stack Overflow
Check contain string, and move the rest element to new list
How to check if a string contains a character in a list
Videos
Say I have a list of strings like: ["Hey dude", "Hey bro", "Sup dude", "Hey bud"]
How do I check if *any* of those strings in the list contain the word "Sup"?Alternatively, in my case, I could also check if *all* of the strings contain "Hey" , which I also don't know how to do.
(Sorry, I'm new to Python)
Edit: Thank you so much for all the help, guys <3
I have two days learning python but I really need to do that code. I also posted this question in StackOverflow but it got downvoted the first second.
I want to make a function to print all the words that contain a specific consonant (only one) and any amount of vowels, but I don't know how to introduce the conditions here.
I made a list with 4 elements via the input method (I know I use a different way), and a method that prints all the words that include the letter "f", but as I said, it needs to be more specific (only one consonant, "f" in this case, and any amount of vowels):
list = []
for x in range (4):
words = str(input("Type a word "))
list.append(words)
#This method kinda works, but it needs to be more specific.
matching = [s for s in list if "f" in s]
print(matching) In my code, if I have words with the letter "f", I'll get a list with all the words containing any amount of "f" letters and any other consonant, but I just want one consonant "f" and vowels, not the other consonants.
My desired result is like
list = [fox, alfa, fa, real, fou]
#['fa', 'fou']
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.
extensionsToCheck = ('.pdf', '.doc', '.xls')
'test.doc'.endswith(extensionsToCheck) # returns True
'test.jpg'.endswith(extensionsToCheck) # returns False
I'm really new to python and i'm having some trouble figuring out how to check if a list contains a certain character.
For example:
list = ['a1', 'a2', 'b1', 'b2', 'a3', 'a5', 'c3']
how would I check to see what elements contain the string 'a'? sorry if i didn't word my question properly i'm still very new to coding, but any help would be appreciated.