Flatten a list/tuple and Call a function with tuples
Jeff
jeffober at gmail.com
Wed Jul 25 15:31:12 EDT 2007
Sorry about that. Hopefully, this should work ;)
def flatten(obj):
if type(obj) not in (list, tuple, str):
raise TypeError("String, list, or tuple expected in
flatten().")
if len(obj) == 1:
if type(obj[0]) in (tuple, list):
return flatten(obj[0])
else:
return [obj[0]]
else:
if type(obj[0]) in (list, tuple):
return flatten(obj[0]) + flatten(obj[1:])
else:
return [obj[0]] + flatten(obj[1:])
x = (1, 2, [3, 4, (5, 6)])
y = ([1, 2, (3, 4)], 5, 6)
z = (1, [2, 3, (4, 5)], 6)
print flatten(x)
print flatten(y)
print flatten(z)
More information about the Python-list
mailing list