Welcome, guest | Sign In | My Account | Store | Cart
# flattens a list eg. flatten(1, 2, ['b','a','c']) = [1, 2, 'a', 'b', 'c']
def flatten(*args):
    for x in args:
        if hasattr(x, '__iter__'):
            for y in flatten(*x):
                yield y
        else:
            yield x

Diff to Previous Revision

--- revision 1 2010-06-07 03:11:12
+++ revision 2 2011-03-01 03:46:26
@@ -1,11 +1,8 @@
 # flattens a list eg. flatten(1, 2, ['b','a','c']) = [1, 2, 'a', 'b', 'c']
 def flatten(*args):
-	x = []
-	for l in args:
-		if not isinstance(l, (list, tuple)): l = [l]
-		for item in l:
-			if isinstance(item, (list,tuple)):
-				x.extend(flatten(item))
-			else:
-				x.append(item)
-	return x
+    for x in args:
+        if hasattr(x, '__iter__'):
+            for y in flatten(*x):
+                yield y
+        else:
+            yield x

History