def flattenlist(L): import types Types =dir(types)[:-5] Types.pop(Types.index('StringType')) Types.pop(Types.index('UnicodeType')) Types.pop(Types.index('StringTypes')) Types.pop(Types.index('ListType')) Types.pop(Types.index('ObjectType')) Types.pop(Types.index('TupleType')) Types = tuple(( eval("types.%s "% i ) for i in Types)) tmp = [] def core(L): if not hasattr(L,'__iter__'): return [L] else : for i in L: if isinstance(i,Types): tmp.append(i) continue if type(i) == type(str()): tmp.append(i) else: core(i) return tmp return core(L) #Examples x=[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[['x']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] >>> flattenlist(x) ['x'] >>> x=(((((1))))) >>> flattenlist(x) [1] >>> x=[(),(),[]] >>> flattenlist(x) [] >>> x=[(1),('1'),[1.0]] >>> flattenlist(x) [1, '1', 1.0] >>> x=[[[[[[(((([1,1]))))]]]]]] >>> flattenlist(x) [1, 1] >>> x=1 >>> flattenlist(x) [1] >>> x=flattenlist >>> flattenlist(x) [<function flattenlist at 0x16a8320>] >>>