Michael Michael - 1 year ago 75
Python Question

Seeing if an element from a 1D array is in another MD array

array1D = ['book', 'aa', 'Ab', 'AB']
arrayMD = ['ss', 'book', 'fd', '2'], ['sw', 'd'], ['we', 'wr']

How could I check to see if any element in array1D exists in arrayMD?

So far I just know of the find() method and that seems to only work for one element search.

Edit: I'd like to get it's index from arrayMD as well

Answer Source

Use in.

for sublist in arrayMD:
    for index, element in enumerate(sublist):
        if element in array1D:
             # Do something
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download