-2

How can I test if one of the multiple items exist in a list using or?

I tried:

data = [[1,'a',4,],['a','b','c'],['c',3,5]...]
for i,val enumerate(data):
    if 'a' or 'b' or 'c' in val:
        data.pop(i)

But it removes the first row only

max jr
  • 7
  • https://stackoverflow.com/questions/16579085/python-verifying-if-one-list-is-a-subset-of-the-other is probably a better duplicate. – Code-Apprentice Feb 04 '18 at 16:15
  • how is this a duplicate? – max jr Feb 04 '18 at 16:16
  • The question that ayhan gave as a duplicate is exactly your problem and shows how to solve it directly. The one I linked provides a better solution without using the `or` operator. – Code-Apprentice Feb 04 '18 at 16:17
  • The mistake in `x or y or z == 0` is the same as the mistake in `'a' or 'b' or 'c' in val`, so yeah, this is a duplicate. Read through the answers and you'll see why. – ayhan Feb 04 '18 at 16:19
  • Somehow everyday there are at least two people who ask the very same question :( – Willem Van Onsem Feb 04 '18 at 16:23

2 Answers2

0

You must test the conditions one at a time:

if 'a' in val or 'b' in val or 'c' in val:

Alternatively, you create two lists, convert them to sets, and test if the first list is a subset of another list. See Python - verifying if one list is a subset of the other for more details.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Try this:

if val in ['a','b','c']:
Kos
  • 4,890
  • 9
  • 38
  • 42
Artier
  • 1,648
  • 2
  • 8
  • 22