2

Can someone please tell me how to check membership of a list in a list.

Such:

if x not in y:

using these values:

y = [[7,1,0][8,8,3][2,4,7]]

x = [7,1,0] # returns false

x = [7,0,0] # returns true

Thanks!

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
James Schinner
  • 1,549
  • 18
  • 28
  • I take it this is Python? If so, you need to tag it as such. Or tag whatever language you are using. It's critical to the question. – lurker Jun 20 '17 at 14:59
  • It's exactly what you wrote: `x in y`. If `x` is a list then equality will be determined by comparing each of the items. – a_guest Jun 20 '17 at 15:01
  • @lurker, first post. I clicked the 'python' tab when creating my post, didn't realize it wasn't implicit. Ta – James Schinner Jun 20 '17 at 22:23

1 Answers1

3

Your list assignment is missing the , between sublists, but everything else should work as expected:

>>> y = [[7,1,0],[8,8,3],[2,4,7]]
>>> x = [7,1,0]
>>> x in y
True
>>> x not in y
False
>>> x = [7,0,0]
>>> x in y
False
>>> x not in y
True
Christian König
  • 3,437
  • 16
  • 28