convert list of lists to list

Fredrik Lundh fredrik at pythonware.com
Tue Jul 22 11:25:43 EDT 2008


antar2 wrote:

> Is there a way to convert list_of_listsA to list_of_listsB, where one
> list in listof lists A is one element of listB?

> list_of_listsA:
> [['klas*', '*', '*'],
> ['mooi*', '*', '*', '*'],
> ['koe'],
> ['arm*', '*', '*(haar)'],
> ['groei*', '*', '*', '*', '*']]
> 
> listB:
> ['klas* * *', 'mooi* * * *, 'koe',  'arm* * * (haar)',  'groei* * * *
> *']

if there's only one level of recursion, and the lists aren't too long, 
you can simply do:

     sum(list_of_lists, [])

(this has quadratic performance, so don't use it for large structures)

for recursive solutions, see:

     http://www.google.com/search?q=flatten+lists+python

</F>




More information about the Python-list mailing list