I have a 2d array in the following format:
[[a,b,c], [a], [c,d], [e]]
My objective is to identify all sublists of length 1 and compare the element in that sublist to elements of other sublists. If the element exists, remove both elements from the list.
In this case we look for 'a' in the 2d array and eliminate 'a,b,c' and 'a' from the 2d array.
The result should be:
[[c,d], [e]]
My code is as follows:
templist = list1 #list1 is the list containing all elements
for i in templist:
if (len(i) == 1):
if(any(i[0] in templist)):
templist.remove(i)
templist.remove([sublist for sublist in mylist if i[0] in sublist])
return templist
On running this code, I get the error - 'bool' object is not iterable. What I am doing wrong and how I can fix my error?
any(i[0] in templist)doesn't make sense.i[0] in templistreturns a boolean, and then you pass it toanywhich expects an iterable.[e]is also a sublist of length 1?