import collections def is_listlike(x): return isinstance(x, collections.Iterable) and not isinstance(x, basestring) def flat(S): result = [] for item in S: if is_listlike(item) and len(item) > 0 and not is_listlike(item[0]): result.append(item) else: result.extend(flat(item)) return result tests = [ [[[[1,2,3] , ['a','b','c']]]], [[[1,2,3] , ['a','b','c']]], [[[1,2,3]] , [['a','b','c']]] ] for S in tests: print(flat(S))
gives
[[1, 2, 3], ['a', 'b', 'c']] [[1, 2, 3], ['a', 'b', 'c']] [[1, 2, 3], ['a', 'b', 'c']]
source share