+3

# list comparison

i want to compare two list and return true if there is any similar element otherwise return false.. code: list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 15] for x in list1: if x in list2: print("True") else: print("False") ............. but it return the output of 5 all comparison. and i want only one output

4/15/2020 8:06:37 PM

Pavan Sirsat

+7

My former version is still dumb though. ๐คฆโโ๏ธ Depending on interpretation it should just be this: print(any(x in list2 for x in list1)) Or this: print(any(a==b for (a, b) in zip(list1, list2))) Right?

+4

I think this is quite simple to implement. Code: list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 15] var = set(list1) & set(list2) #if var == None: # print("False") #else: #print("True") The commented part should be replaced with : Print(bool(var)) This is okay. Thanks for the notice petenera_

+2

You can use some simple math logic with set() and len() to get the answer without doing any actual comparison of element to element. # remove any repeated elements from # each list and add their lengths together a = len(set(lst1)) + len(set(lst2)) # combine both lists together and use set # to remove repeated elements and get # the length of combined list b = len(set(lst1+lst2)) # if a not equal to b then there is at least 1 # element that is the same in each list if a != b: print(True) else: print(False) Just a different approach.

+1

HonFu John Robotane i'm now confused i think everyone understood the question in a different way. I hope he can open and clear our doubts

+1

list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 15] c=0 for x in list1: if x in list2: c+=1 if c>0: print("True") else: print("False") #Plz run the above code ,and u will get your desired output

+1

list1 = [1, 2, 3, 4, 5] list2 = [5, 6, 7, 8, 15] for x in list1: if x in list2: print("True") break else: print("False") break Ths will work for u Pavan Sirsat add break .

+1

I think declare a variable d, d++ with each Iteration that true, if d>0 print true, else print false.

+1

Subhojit Sikdar ๐๐

+1

Koffi Cobbin your code is always going to return True, no matter what's inside the lists

0

for x in list1: if x not in list2: print("false") break else: print("true") note that the else here belongs to the for loop not to the if, so take care from the indentation, the for-loop else is a statement that will be excuted only if the for-loop ended without any break statement EDIT: you can also write print(lst1==lst2) and it return either True or False

0

Thanks Mo Hani

0

Pavan Sirsat Also keep in mind that (lst1==lst2) will return False if the two lists have the same elements but different order, however the for-loop will neglect the order but instead look at the elements only, so make sure to choose from either according to what you want to achieve.

0

This would be my version: print( True if any( a==b for a, b in zip(list1, list2) ) else False )

0

I think it could also be solve with print(set(list1) == set(list2))

0

HonFu i don't think "any" should be used for that, it will simply return true if ONLY two corresponding elements are equal and won't bother to look at the rest of list, where [4,5,6] & [4,9,3] will return True although it should be False, i think the use of "all" instead of "any" would be more reasonable John Robotane Also the use of set will always result in an ordered array, so your approach will return True if the two lists have the same elements but it won't bother to look at the order.

0

Mo Hani , I thought he wanted to compare two lists regardless the order of the elements.

0

Wait - sorry, I just wrote nonsense. I hope you forgive me for erasing it. ๐

0

HonFu My bad, i read the question late yesterday and thought he meant he wanted to compare the whole lists, didn't notice he mentioned that he only wants to return True if "any" two elements are equal. So for that use your approach is the closer.

0

yes, but I think HonFu understood it the right way!

0

HonFu The difference between the two statements is that the first won't look at the order just scanning the whole 2 arrays and checking if there is a repeated element in any position while the second makes sure that the equal elements are also corresponding in same position, due to "zip" , so i think it also comes to what he wants to achieve