1

I have a list:

myList = [abc123, def456, ghi789, xyz999]

I need to search specific values in myList, based on this "sub-list" of allowed values:

allowed = [abc123, xyz999]

Edit: I need to check if myList contains the values in allowed

I already know how to seach for a specific substring in a list but I don't really understand how to do the same with a "sub-list".

Thank you in advance for your help.

Sergio La Rosa
  • 495
  • 8
  • 18
  • Possible duplicate of [elegant find sub-list in list](http://stackoverflow.com/questions/10106901/elegant-find-sub-list-in-list) – lch Apr 20 '17 at 13:49
  • What do you mean by "search"? Do you want to check if `myList` contains the values in `allowed`? Or do you want to find their index in `myList`? Please add the expected output to the question. – Aran-Fey Apr 20 '17 at 13:49
  • Yes, I want to check if myList contains the values in allowed. – Sergio La Rosa Apr 20 '17 at 13:50
  • @PantsuJo So what's the expected output? `[True, True]` or maybe just `True` or something else? – Aran-Fey Apr 20 '17 at 13:51
  • Just "true", a confirm that the allowed values exist in myList. – Sergio La Rosa Apr 20 '17 at 13:52
  • looks like you checking not allowed, but instead mandatory values. – Serge Apr 20 '17 at 13:55
  • Or swap the lists (larger for allowed, smaller for observed) – Serge Apr 20 '17 at 13:56
  • Or may be: You need to check if myList contains ONLY the values from allowed, or may be You need to check if myList contains ALL the values from allowed, or may be You need to check if myList contains AT LEAST one values from allowed – Serge Apr 20 '17 at 14:18

4 Answers4

2

I am not sure what you are trying to achieve, but I think you mean to look if the allowed values are in your list. So try this:

myList = ['abc123', 'def456', 'ghi789', 'xyz999']
allowed = ['abc123', 'xyz999']

for i in myList:
    if i in allowed:
        print("{0} in the list".format(i))

After reading your comments, the question is a bit vague, I made a solutions using a Dictionary to store the values of allowed and check wether all of them are in myList

myList = ['abc123', 'def456', 'ghi789', 'xyz999']
allowed = {'abc123' : False, 'xyz999' : False}

for i in myList:
    if i in allowed:
        allowed[i]=True

print((all(value == True for value in allowed.values())))
Ludisposed
  • 1,709
  • 4
  • 18
  • 38
2

You can use set.issubset:

>>> set(allowed).issubset(myList)
True

Or, if your values aren't hashable, all with a comprehension:

>>> all(value in myList for value in allowed)
True
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
1
for value in myList:
    if value in allowed:
        #dosomething
Daniel Frühauf
  • 311
  • 1
  • 2
  • 12
1

Assuming string lists (i.e. hashable):

set(a).intersect(set(b)) yield common elements

if need only boolean use issubset method: Python - verifying if one list is a subset of the other

for indvidual item search find, index Python: Find in list

Community
  • 1
  • 1
Serge
  • 3,387
  • 3
  • 16
  • 34