By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,357 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,357 IT Pros & Developers. It's quick & easy.

convert list of lists to list

P: n/a
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
Share this Question
Share on Google+
4 Replies

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.