Latest recipes tagged "flatten" Code RecipesFlattening an arbitrarily nested list in Python (Python) 2014-10-05T21:44:45-07:00Vasudev Ram <p style="color: grey"> Python recipe 578948 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a recipe to flatten a Python list which may have nested lists as items within it. It works for lists that have a maximum depth of nesting roughly equal to the recursion depth limit of Python, which is set to 1000 by default, though it can be increased with sys.setrecursionlimit().</p> Python - from nD array to flat array (Python) 2014-03-21T16:09:39-07:00Roberto Bello <p style="color: grey"> Python recipe 578854 by <a href="/recipes/users/4189498/">Roberto Bello</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/nd/">nd</a>). </p> <p>From a multidimensional array to a flat array avoiding numpy. The code could be better?</p> Flattening an arbitrarily deep list (or any iterator) (Python) 2012-04-03T17:13:35-07:00Garrett <p style="color: grey"> Python recipe 578092 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tuple/">tuple</a>). Revision 6. </p> <p>What if you had a list like this: [1, -10, [1,2,[3,4]], xrange(200)], and you just wanted to go through each element in order (wanted it to return a simple list of [1,-10,1,2,3,4,1,2,3,4...199])</p> <p>I've seen a couple of attempts to flatten arbitrarily deep lists. Many of them involve recursion, like this one: <a href="" rel="nofollow"></a></p> <p>Recursion is generally considered non-pythonic (at least to my knowledge), so I have used one which just involves simple iterators instead. Also, recursion will fail if the list is too deep (so it wouldn't really be arbitrary, would it?).</p> Flatten Array/Tuple (Python) 2011-10-31T10:53:39-07:00Luca Zarotti <p style="color: grey"> Python recipe 577932 by <a href="/recipes/users/4179728/">Luca Zarotti</a> (<a href="/recipes/tags/array/">array</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Flatten a nested array/tuple</p> Flatten XML to XPath syntax lines (Python) 2011-01-19T16:05:24-08:00Raphaël Jolivet <p style="color: grey"> Python recipe 577547 by <a href="/recipes/users/4135673/">Raphaël Jolivet</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/grep/">grep</a>, <a href="/recipes/tags/sed/">sed</a>, <a href="/recipes/tags/xml/">xml</a>, <a href="/recipes/tags/xpath/">xpath</a>). </p> <p>This script acts like <a href="">xml2</a>. It transforms a XML file into a flat text output, with <em>XPath</em>-like syntax, one line per XML node or attribute. This format is more suitable for working with standard unix CLI utils (sed, grep, ... etc).</p> Flattening lists (Python) 2010-12-21T16:04:38-08:00Michael Puckett <p style="color: grey"> Python recipe 577509 by <a href="/recipes/users/4176295/">Michael Puckett</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>). </p> <p>Classic / frequently asked, <em>'How to flatten a list, regardless the nesting.'</em></p> Fast flatten() with depth control and oversight over which subtrees to expand (Python) 2010-11-26T11:10:01-08:00Kevin L. Sitze <p style="color: grey"> Python recipe 577470 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/iterators/">iterators</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimal_solution/">optimal_solution</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/tuple/">tuple</a>). </p> <p>Extremely fast, non-recursive, depth limited flatten with powerful control over which subtrees are to be expanded. If this is what you need then look no further.</p> Flatten a list (or list of lists, etc.) (Python) 2011-03-01T03:46:26-08:00Sunjay Varma <p style="color: grey"> Python recipe 577255 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/extend/">extend</a>, <a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/smart/">smart</a>). Revision 2. </p> <p>I created this function sometime ago for my own purposes. It simply flattens a list.</p> <p>Check out the first comment for another version of someone else's design.</p> Flatten a list (Python) 2010-06-18T03:45:20-07:00Giannis Fysakis <p style="color: grey"> Python recipe 577250 by <a href="/recipes/users/4174072/">Giannis Fysakis</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>). Revision 3. </p> <p>Flatten a list </p> flatten sequences (Python) 2008-09-05T08:41:49-07:00Christophe Simonis <p style="color: grey"> Python recipe 576487 by <a href="/recipes/users/4166953/">Christophe Simonis</a> (<a href="/recipes/tags/flatten/">flatten</a>, <a href="/recipes/tags/list/">list</a>). Revision 3. </p> <p>A simple method to flatten a sequence (list, tuple...)</p>