When it is required to concatenate a matrix vertically, the list comprehension can be used.
Below is a demonstration of the same −
Example
from itertools import zip_longest my_list = [["Hi", "Rob"], ["how", "are"], ["you"]] print("The list is : ") print(my_list) my_result = ["".join(elem) for elem in zip_longest(*my_list, fillvalue ="")] print("The list after concatenating the column is : ") print(my_result)
Output
The list is : [['Hi', 'Rob'], ['how', 'are'], ['you']] The list after concatenating the column is : ['Hihowyou', 'Robare']
Explanation
The required packages are imported.
A list of list is defined, and is displayed on the console.
The list comprehension is used to zip the elements, and join them by eliminating the empty spaces.
This is assigned to a variable.
This variable is displayed as output on the console.