Skip to content Skip to sidebar Skip to footer

Python: Concatenating Strings Of A List Within A List

Is it possible to take each separate string from each list and combine it into one string, then have a list of strings? Instead of a list of strings within a list? names = ['red',

Solution 1:

You are overwriting names each loop, hence the last value of names is 'white farm house'.

Try this instead:

l_out = [' '.join(x) for x in names]
print(l_out)

Output:

['red barn', 'barn', 'front porch', 'white farm house']

Or you can do it the way you're trying:

l_out = []
for name in names:
    l_out.append(' '.join(name))
print(l_out)

Output:

['red barn', 'barn', 'front porch', 'white farm house']

Post a Comment for "Python: Concatenating Strings Of A List Within A List"