-2

I have a list of list like this:

l = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

and I want to check whether one of the lists in the list of lists have say integer 3 and 5 in the same array. So in my example here, l[1] has both integers 3 and 5. How would I go about finding that?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 8
    Anything you've tried so far? Please update the question with your code. – 0buz Feb 13 '20 at 14:22
  • 2
    If you just want a boolean "does it exist", something like `any((3 in x and 5 in x) for x in l)` should work. – 0x5453 Feb 13 '20 at 14:22
  • 6
    How would you do it with a simple list? Try to find a solution for that task and then loop over this code. – Matthias Feb 13 '20 at 14:22
  • Does this answer your question? [Can Python test the membership of multiple values in a list?](https://stackoverflow.com/questions/6159313/can-python-test-the-membership-of-multiple-values-in-a-list) – Gino Mempin Feb 14 '20 at 03:38

2 Answers2

1

This should work:

l = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]

if 3 in l[1] and 5 in l[1]:
    print("both numbers are in this array", l[1])
kaya3
  • 47,440
  • 4
  • 68
  • 97
0

Use enumerate() for indexing.

IDLE Output :

>>> l = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> integer1 = 3
>>> integer2 = 5

>>> for index, nested_list in enumerate(l):
    if integer1 in nested_list and integer2 in nested_list:
        print(f"Both integers are in l[{index}]")
        print("List is " + str(l[index]))


Both integers are in l[1]
List is [3, 4, 5]
gsb22
  • 2,112
  • 2
  • 10
  • 25