pythonlistmembershipdimensional

Check membership of list in two dimensional list


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!


Solution

  • 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