list comparison | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

15th Apr 2020, 8:06 PM
Pavan Sirsat
Pavan Sirsat - avatar
38 Answers
+ 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?
16th Apr 2020, 10:18 AM
HonFu
HonFu - avatar
+ 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_
17th Apr 2020, 1:21 PM
Koffi Cobbin
Koffi Cobbin - avatar
+ 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.
16th Apr 2020, 6:11 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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
16th Apr 2020, 10:13 AM
Mo Hani
Mo Hani - avatar
+ 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 .
16th Apr 2020, 6:08 PM
Vijay(v-star🌟)
Vijay(v-star🌟) - avatar
+ 1
I think declare a variable d, d++ with each Iteration that true, if d>0 print true, else print false.
17th Apr 2020, 10:09 AM
Hiếu Võ Trần Minh
Hiếu Võ Trần Minh - avatar
+ 1
17th Apr 2020, 11:46 AM
petenera_
petenera_ - avatar
+ 1
Koffi Cobbin your code is always going to return True, no matter what's inside the lists
17th Apr 2020, 1:53 PM
petenera_
petenera_ - avatar
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
15th Apr 2020, 8:11 PM
Mo Hani
Mo Hani - avatar
0
Thanks Mo Hani
15th Apr 2020, 8:16 PM
Pavan Sirsat
Pavan Sirsat - avatar
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.
15th Apr 2020, 8:21 PM
Mo Hani
Mo Hani - avatar
0
This would be my version: print( True if any( a==b for a, b in zip(list1, list2) ) else False )
15th Apr 2020, 10:33 PM
HonFu
HonFu - avatar
0
I think it could also be solve with print(set(list1) == set(list2))
15th Apr 2020, 10:49 PM
John Robotane
John Robotane - avatar
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.
16th Apr 2020, 10:00 AM
Mo Hani
Mo Hani - avatar
0
Mo Hani , I thought he wanted to compare two lists regardless the order of the elements.
16th Apr 2020, 10:10 AM
John Robotane
John Robotane - avatar
0
Wait - sorry, I just wrote nonsense. I hope you forgive me for erasing it. 😂
16th Apr 2020, 10:11 AM
HonFu
HonFu - avatar
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.
16th Apr 2020, 10:11 AM
Mo Hani
Mo Hani - avatar
0
yes, but I think HonFu understood it the right way!
16th Apr 2020, 10:16 AM
John Robotane
John Robotane - avatar
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
16th Apr 2020, 10:25 AM
Mo Hani
Mo Hani - avatar
0
I think it's print(any(x in list2 for x in list1)) with zip, you'll test if elements with same index are equal, isn't it? but it looks like he wants to return True if the two share a common element, else false.
16th Apr 2020, 10:28 AM
John Robotane
John Robotane - avatar