<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">The idea of flattening a object or datatype is a functional programming technique and not just a part of Ruby and Mathematica According to this answer on the programming stack exchange there is no method / function that implements flatten for build in Python functions but you can get almost there with <span class="">itertools.chain.from_iterable . See </span><a href="http://programmers.stackexchange.com/questions/254279/why-doesnt-python-have-a-flatten-function-for-lists" class="">http://programmers.stackexchange.com/questions/254279/why-doesnt-python-have-a-flatten-function-for-lists</a> .<div><blockquote type="cite" class=""><div class="">On Feb 15, 2016, at 4:12 PM, Lewit, Douglas <<a href="mailto:d-lewit@neiu.edu" class="">d-lewit@neiu.edu</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><font size="4" class="">Hi everyone,</font><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Well it's President's Day and I've got the day off! Hooray!!! Finally some time to just relax and mess around. So I'm at my computer playing around with Python and wondering how to resolve the issue of multiple lists embedded within other lists. I came up with two functions that I think solve the problem. But I was wondering if Guido or someone else added a builtin function or method that does this automatically for the programmer. Or is there an easier way? Okay.... thanks. ( In case you're wondering why I called the function "flatten" it's because I know from experience that Wolfram Mathematica and Ocaml have these "flatten" functions. I think Ruby has something similar, but I haven't played with Ruby in a while so I'm not really sure. ) The try: except block is important because you can't subscript non-list data structures in Python. The IndexError is what you get when you try to index an empty list. So I ****think**** my try: except block covers most commonly encountered exceptions when working with lists embedded within other lists.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Best,</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" class="">Douglas.</font></div><div class=""><font size="4" class=""><br class=""></font></div><div class=""><font size="4" face="arial, helvetica, sans-serif" class=""><div class=""><font color="#0000ff" class="">def flatten(lst):</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>if lst == [ ]:</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>return lst</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>else:</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>try:</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>return [lst[0][0]] + flatten(lst[0][1:] + lst[1:])</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>except TypeError:</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>return [lst[0]] + flatten(lst[1:])</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>except IndexError:</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>return flatten(lst[1:])</font></div><div class=""><br class=""></div><div class=""><div class=""><font color="#0000ff" class="">def flattenAgain(lst):</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>newList = lst[:]</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>while newList != flatten(newList):</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>newList = flatten(newList)</font></div><div class=""><font color="#0000ff" class=""><span class="" style="white-space:pre"> </span>return newList</font></div></div><div class=""><br class=""></div><div class=""><br class=""></div></font></div></div>
_______________________________________________<br class="">Chicago mailing list<br class=""><a href="mailto:Chicago@python.org" class="">Chicago@python.org</a><br class="">https://mail.python.org/mailman/listinfo/chicago<br class=""></div></blockquote></div><br class=""></body></html>