Is there a straightforward way to remove a list containing all elements the same instead of specifying the actual location? For example, A[1] has to be removed because all the elements are the same?
A=[[[1],[2],[2]],[[3],[3],[3]],[[4],[5],[4]]]
print(A)
A.remove(A[1])
print(A)
The output is and should be
[[[1],[2],[2]],[[4],[5],[4]]]
>Solution :
We can use a list comprehension here:
A = [[[1],[2],[2]],[[3],[3],[3]],[[4],[5],[4]]]
output = [x for x in A if min(x) != max(x)]
print(output) # [[[1], [2], [2]], [[4], [5], [4]]]
We can identify a sublist as candidate for being removed by checking if the min and max values be the same. If those two values are not the same, then we retain the sublist.