Reputation: 133
I'm trying to build a function to check 2 lists of tuples, and return True if at least one of the tuples is in the other list:
def g32(x, y):
for i in range(len(x)):
for j in range(len(y)):
if x[i] in y[j]:
return True
return False
g32( [(3,5),(2,8)] , [(6,5),(3,5),(7,2)])
False
Expected True since (3,5) is in both lists, ideas? Thanks
p.s would like to use simple syntax like this since it's for school and some built in python functions can't be used
Upvotes: 0
Views: 36
Reputation: 244301
If you use the in
operator in a tuple, it will return the elements of this. In your case you will be comparing: if (3, 5) in 3:
and if (3, 5) in 5:
, in each case returns False
.
Must be use:
def g32(x, y):
for i in range(len(x)):
if x[i] in y:
return True
return False
print(g32( [(3,5),(2,8)] , [(6,5),(3,5),(7,2)]))
Output:
True
Suggestion:
def g32(x, y):
for X in x:
if X in y:
return True
return False
If there is a change of order:
def g32(x, y):
for X in x:
if X in y:
return True
if (X[1], X[0]) in y:
return True
return False
print(g32( [(3,5),(2,7),(4,4),(8,3)] , [(6,5),(3,3),(7,2)] ))
Output:
True
Upvotes: 1