10

I have written a function to check for the existence of a value in a list and return True if it exists. It works well for exact matches, but I need for it to return True if the value exists anywhere in the list entry (e.g. value <= listEntry, I think.) Here is the code I am using for the function:

def isValInLst(val,lst):
    """check to see if val is in lst.  If it doesn't NOT exist (i.e. != 0), 
    return True. Otherwise return false."""
    if lst.count(val) != 0:
        return True
    else:
        print 'val is '+str(val)
        return False

Without looping through the entire character string and/or using RegEx's (unless those are the most efficient), how should I go about this in a pythonic manner?

This is very similar to another SO question, but I need to check for the existence of the ENTIRE val string anywhere in the list. It would also be great to return the index / indices of matches, but I'm sure that's covered elsewhere on Stackoverflow.

3
  • I forgot to mention that I also tried a feeble attempt at a list comprehension using any() and all() with no success. It just always returns True, so I assume it's matching any (or all in no order) substring characters to list entries. Here is what I tried: if any(x in b for x in a): return True Commented Jul 2, 2013 at 17:35
  • Can you add your code on any() and all() here, so that I can look into it Commented Jul 2, 2013 at 17:38
  • It’s just the other way around: b in x for x in a instead of x in b (you want to check if your key is in any of the list’s elements). Commented Jul 2, 2013 at 17:44

4 Answers 4

24

If I understood your question then I guess you need any:

return any(val in x for x in lst)

Demo:

>>> lst = ['aaa','dfbbsd','sdfdee']
>>> val = 'bb'
>>> any(val in x  for x in lst)
True
>>> val = "foo"
>>> any(val in x  for x in lst)
False
>>> val = "fde"
>>> any(val in x  for x in lst)
True
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, that's exactly what I needed to do. I had my list comprehension variables backwards. Out of curiosity, any() and all() both return true, but my val does not exists as a substring of every list iterable. I did not expect that. Why would all(val in x for x in lst) return true?
@TheProletariat are you sure your substring isn't in every list item? Try using my function lower down and check if it returns all indices of the list to be sure.
@TheProletariat all() will return True only if the substring is found in all list items, check your list again.
to find any exact matches it would be, return any(val == x for x in lst)
Agree to point by @InnocentBystander : the any in x for x in lst should be used with care, since article__text matches article__textboxes etc.
3

Mostly covered, but if you want to get the index of the matches I would suggest something like this:

indices = [index for index, content in enumerate(input) if substring in content]

if you want to add in the true/false you can still directly use the result from this list comprehension since it will return an empty list if your input doesn't contain the substring which will evaluate to False.

In the terms of your first function:

def isValInLst(val, lst):
    return bool([index for index, content in enumerate(lst) if val in content])

where the bool() just converts the answer into a boolean value, but without the bool this will return a list of all places where the substring appears in the list.

Comments

1

There are multiple possibilities to do that. For example:

def valInList1 (val, lst):
    # check `in` for each element in the list
    return any(val in x for x in lst)

def valInList2 (val, lst):
    # join the list to a single string using some character
    # that definitely does not occur in val
    return val in ';;;'.join(lst)

Comments

-1
s = input().strip() 

if 'm' in s:
    print('No')
else:
    print('Yes')

2 Comments

Thank you for your interest in contributing to the Stack Overflow community. This question already has existing answers. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?
The question is about searching on a list of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.