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 PMPavan Sirsat
41 AnswersNew Answer
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?
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_
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.
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
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
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 .
I think declare a variable d, d++ with each Iteration that true, if d>0 print true, else print false.
Subhojit Sikdar 👌👍
Koffi Cobbin your code is always going to return True, no matter what's inside the lists
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
Thanks Mo Hani
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.
This would be my version: print( True if any( a==b for a, b in zip(list1, list2) ) else False )
I think it could also be solve with print(set(list1) == set(list2))
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.
Mo Hani , I thought he wanted to compare two lists regardless the order of the elements.
Wait - sorry, I just wrote nonsense. I hope you forgive me for erasing it. 😂
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.
yes, but I think HonFu understood it the right way!
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