You could test for contained lists before flattening:
def flatten(a):
b = []
for c in a:
if isinstance(c, list) and any(isinstance(i, list) for i in c):
b.extend(flatten(c))
else:
b.append(c)
return b
Demo:
>>> def flatten(a):
... b = []
... for c in a:
... if isinstance(c, list) and any(isinstance(i, list) for i in c):
... b.extend(flatten(c))
... else:
... b.append(c)
... return b
...
>>> A = [ [[1,3]], [[3,5], [4,4], [[5,3]]] ]
>>> flatten(A)
[[1, 3], [3, 5], [4, 4], [5, 3]]
This tries to be as efficient as can be under the circumstances; any()
only needs to test until a list is found, not all elements.