469,641 Members | 1,501 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

convert list of lists to list

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* * * *
*']

Thankx!
Jul 22 '08 #1
4 1813
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>

Jul 22 '08 #2
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* * * *
*']
>>outer = [['klas*', '*', '*'],
.... ['mooi*', '*', '*', '*'],
.... ['koe'],
.... ['arm*', '*', '*(haar)'],
.... ['groei*', '*', '*', '*', '*']]
>>[" ".join(inner) for inner in outer]
['klas* * *', 'mooi* * * *', 'koe', 'arm* * *(haar)', 'groei* * * * *']

Peter
Jul 22 '08 #3
Fredrik Lundh wrote:
>['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, [])
oops. that's what you get for taking the subject line too literally...

Jul 22 '08 #4
On Jul 22, 8:25*am, Fredrik Lundh <fred...@pythonware.comwrote:
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 linear performance, you can use itertools:

list(itertools.chain(*list_of_lists))

Raymond
Jul 22 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Jochen Hub | last post: by
19 posts views Thread by Oliver Eichler | last post: by
16 posts views Thread by flyaflya | last post: by
reply views Thread by Andre Winarko | last post: by
3 posts views Thread by rh0dium | last post: by
36 posts views Thread by pereges | last post: by
2 posts views Thread by Fred Mellender | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.