6

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.

Community
  • 1
  • 1
TheProletariat
  • 916
  • 2
  • 11
  • 23
  • 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 – TheProletariat Jul 02 '13 at 17:35
  • Can you add your code on any() and all() here, so that I can look into it – user1050619 Jul 02 '13 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). – poke Jul 02 '13 at 17:44

3 Answers3

18

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
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 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 Jul 02 '13 at 17:45
  • @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. – Slater Victoroff Jul 02 '13 at 17:48
  • @TheProletariat `all()` will return `True` only if the substring is found in all list items, check your list again. – Ashwini Chaudhary Jul 02 '13 at 17:58
2

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.

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
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)
poke
  • 369,085
  • 72
  • 557
  • 602