user1687891
user1687891

Reputation: 864

How to check if the array is available inside the multi dimensional array

I have a multi dimensional array like this

A =  [[19, 16], [3], [8], [10], [11, 18]]

I want to check if my new generated array is available inside A

Eg. if B = [11, 18] 

is available inside A or not.

Upvotes: 0

Views: 52

Answers (4)

mss
mss

Reputation: 1493

Either your run ' for ' or ' while ' loop or just use Python keyword ' in ' to check:

A =  [[19, 16], [3], [8], [10], [11, 18]]

for i in A:
    if([11,18]==i):
        print "found"
        break
#or 
for i in range(0,len(A)):
    if(A[i]==[11,19]):
        print "found"
        break  
#or
i=0
while(i<len(A)):
    if(A[i]==[11,18]):
        print "found"
        break
    i+=1

or

A =  [[19, 16], [3], [8], [10], [11, 18]]
if [11,18] in A:
    print "found"

Upvotes: 0

Rushil Srivastava
Rushil Srivastava

Reputation: 297

Simply just use the in operator.

print([11, 18] in A)

Result:

true

Upvotes: 0

Mika72
Mika72

Reputation: 411

A =  [[19, 16], [3], [8], [10], [11, 18]]

B = [11, 18]

print(B in A)

or

if B in A:
   #do something
   print("Found")

Upvotes: 0

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48357

Just use in operator.

A =  [[19, 16], [3], [8], [10], [11, 18]]
B = [11, 18] 
print(B in A)

Output

true

Upvotes: 2

Related Questions