convert list of lists to list

A

antar2

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!
 
F

Fredrik Lundh

antar2 said:
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>
 
P

Peter Otten

antar2 said:
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
 
F

Fredrik Lundh

Fredrik said:
['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...
 
B

Bruno Desthuilliers

antar2 a écrit :
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* * * *
*']

Your example doesn't match your description. Assuming the example is
correct and the description incorrect:

listB = [' '.join(sublist) for sublist in list_of_listsA]


NB : totally irrelevant, but your naming convention sucks big time IMHO.
 
R

Raymond Hettinger

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top