From given three array find the duplicates which have same attributes in all the three arrays. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

From given three array find the duplicates which have same attributes in all the three arrays.

It runs fine but it prints 1 more if more than one duplicate is present. https://code.sololearn.com/cq6PI2W1T5bP/?ref=app

21st Aug 2019, 5:48 PM
Geek
Geek - avatar
5 Answers
+ 2
print(len(l1)-len(set([tuple(i) for i in l1])))
21st Aug 2019, 6:32 PM
Diego
Diego - avatar
+ 1
Thanks it work. But can you explain that?
21st Aug 2019, 9:10 PM
Geek
Geek - avatar
+ 1
Ok thanks. Why should I convert it to tuple. Why It shows error when I use len(set(l1))
22nd Aug 2019, 2:14 AM
Geek
Geek - avatar
+ 1
In simple terms, lists can't be used inside sets, but tuples can.
22nd Aug 2019, 2:26 AM
Diego
Diego - avatar
0
[tuple(i) for i in l1] converts every element of l1 into a tuple. [[1, 2], [3, 4]] -> [(1, 2), (3, 4)] set(...) removes all duplicates (it can do much more than that). len(l1)-len(set(...)) returns the total number of items minus the number of unique items, resulting in the number of duplicate items.
21st Aug 2019, 10:18 PM
Diego
Diego - avatar