Here's a quick flatten() function: 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: return [obj[0]] + flatten(obj[1:])
x = [1, 2, (3, 4)] y = (1, 2, [3, 4]) z = "It even works with strings!" d = {"foo": "bar", "baz": "bat"} print flatten(x) print flatten(y) print flatten(z) print flatten(d) -- http://mail.python.org/mailman/listinfo/python-list