[FRPythoneers] Joining a lists of lists?

Mike Olson Mike.Olson at fourthought.com
Tue Aug 6 15:42:19 MDT 2002


On Tue, 2002-08-06 at 15:38, marciot wrote:
> Hi,
> 
> I was looking for an easy way to convert a list of lists into a flattened
> list:
> 
> >>> a = somefunc([[1,2,3],[4,5,6],[7,8,9]])
> >>> print a
> [1,2,3,4,5,6]

There are many ways to do this.  It does depend, do you need uniqueness
in the result set?

If so then something like

def joinlists(lists):
  res = {}
  for l in lists:
     for v in l:
        res[v] = 1
  k = res.keys()
  k.sort() #optional
  return k

If not, then something like:

>>> t = [[1,2,3],[4,5,6],[7,8,9]]
>>> reduce(lambda x,y:x+y,t,[])

As for a place to find Lisp translations, I'm not sure.  However,
activestate does keep a "cookbook" of python recipies.

Mike

> 
> In scheme, I would have written:
> 
>    (apply #'append '((1 2 3) (4 5 6) (7 8 9))).
> 
> What's the equivalent expression in Pythonese?
> 
> While I'm at it, is there any good online reference that shows python 
> equivalents for common Scheme/LISP expressions?
> 
> Marcio Luis Teixeira
> 
> 
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
-- 
Mike Olson                                Principal Consultant
mike.olson at fourthought.com                +1 303 583 9900 x 102
Fourthought, Inc.                         http://Fourthought.com 
4735 East Walnut St,                      http://4Suite.org
Boulder, CO 80301-2537, USA
XML strategy, XML tools, knowledge management




More information about the FRPythoneers mailing list