Sriyan Fernando
Sriyan Fernando

Reputation: 11

making a list of lists and strings into a flat list of strings

I have the following lista that contains lists and strings:

[['i','love','you'],['i','like','you']]

I would like to make it a list with this output:

["i love you", "i like you"]

Probably I might not be using the correct keywords to find the desired answer(Using python).how to solve this?

Upvotes: 0

Views: 63

Answers (2)

DYZ
DYZ

Reputation: 57033

The same function as in the other answer, but uses mapping:

strings = [['i','love','you'],['i','like','you']]
list(map(" ".join, strings))
#['i love you', 'i like you']

Upvotes: 0

Loocid
Loocid

Reputation: 6441

You can use .join() to flatten a single list into a string. Then use list comprehension to iterate over all the lists.

inp = [['i','love','you'],['i','like','you']]

outp = [' '.join(i) for i in inp]

print(outp)

>>> ['i love you', 'i like you']

Upvotes: 3

Related Questions