Search code examples
pythonarraysconcatenation

How can I concatenate items in a 2 dimensional array to a single string?


Let's say I have a 2d array like so

T = [["w", 'b'],["y",'b','w'],["f","z"]]

For a one dimensional array I know I would do something like T2 = ' '.join(T) but it doesn't work the same way on a 2d array, I tried to iterate through the array but to no avail.

I want to concatenate the items inside of each separate array inside T. How can I do this?


Solution

  • If you're trying to concatenate the strings in each individual inner list you can do something like

    T2 = [' '.join(a) for a in T]
    

    or

    T2 = list(map(' '.join, T))
    

    Either of these will spit out a list of strings consisting of the concatenated strings of the inner list.