[Baypiggies] Baypiggies snippets

Keith Dart ♂ keith at dartworks.biz
Thu Mar 22 01:17:14 CET 2007


Maybe Baypiggies can have a Python snippets night? or a snippets area
on the web site.

Here is one I recently did. Any comments? Improvements?

def flatten(alist):
    rv = []
    for val in alist:
        if isinstance(val, list):
            rv.extend(flatten(val))
        else:
            rv.append(val)
    return rv


This takes a list that may have embedded lists, to any depth, and
flattens it to a single list.



-- 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Keith Dart <keith at dartworks.biz>
   public key: ID: 19017044
   <http://www.dartworks.biz/>
   =====================================================================



More information about the Baypiggies mailing list