Given a String Matrix, perform column-wise concatenation of strings, handling variable lists lengths.
Input : [[“Gfg”, “good”], [“is”, “for”]]
Output : [‘Gfgis’, ‘goodfor’]
Explanation : Column wise concatenated Strings, “Gfg” concatenated with “is”, and so on.
Input : [[“Gfg”, “good”, “geeks”], [“is”, “for”, “best”]]
Output : [‘Gfgis’, ‘goodfor’, “geeksbest”]
Explanation : Column wise concatenated Strings, “Gfg” concatenated with “is”, and so on.
Method #1: Using loop
This is brute way in which this task can be performed. In this, we iterate for all the columns and perform concatenation.
Python3
test_list = [[ "Gfg" , "good" ], [ "is" , "for" ], [ "Best" ]]
print ( "The original list : " + str (test_list))
res = []
N = 0
while N ! = len (test_list):
temp = ''
for idx in test_list:
try : temp = temp + idx[N]
except IndexError: pass
res.append(temp)
N = N + 1
res = [ele for ele in res if ele]
print ( "List after column Concatenation : " + str (res))
|
Output
The original list : [['Gfg', 'good'], ['is', 'for'], ['Best']]
List after column Concatenation : ['GfgisBest', 'goodfor']
Time Complexity: O(n2)
Space Complexity: O(n)
Method #2 : Using join() + list comprehension + zip_longest()
The combination of above functions can be used to solve this problem. In this, we handle the null index values using zip_longest, and join() is used to perform task of concatenation. The list comprehension drives one-liner logic.
Python3
from itertools import zip_longest
test_list = [[ "Gfg" , "good" ], [ "is" , "for" ], [ "Best" ]]
print ( "The original list : " + str (test_list))
res = [" ".join(ele) for ele in zip_longest(*test_list, fillvalue =" ")]
print ( "List after column Concatenation : " + str (res))
|
Output
The original list : [['Gfg', 'good'], ['is', 'for'], ['Best']]
List after column Concatenation : ['GfgisBest', 'goodfor']
Time Complexity: O(n2) -> (loop+join)
Space Complexity: O(n)
Method #3: Using numpy.transpose() and numpy.ravel()
Step-by-step approach:
- Import the numpy library.
- Initialize the list.
- Find the maximum length of a sublist using a list comprehension and the max() function.
- Pad each sublist with empty strings to make them the same length using another list comprehension.
- Convert the padded list to a numpy array using the np.array() function.
- Use the transpose (T) method to switch rows and columns.
- Use a list comprehension and join to concatenate the strings in each row of the transposed array.
- Print the result.
Below is the implementation of the above approach:
Python3
import numpy as np
test_list = [[ "Gfg" , "good" ], [ "is" , "for" ], [ "Best" ]]
max_len = max ( len (sublist) for sublist in test_list)
padded_list = [sublist + [''] * (max_len - len (sublist)) for sublist in test_list]
arr = np.array(padded_list)
arr_t = arr.T
res = [''.join(row) for row in arr_t]
print ( "List after column concatenation: " + str (res))
|
OUTPUT:
List after column concatenation: ['GfgisBest', 'goodfor']
Time complexity: O(n^2), where n is the number of elements in the input list.
Auxiliary space: O(n), for the numpy array and the padded list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Mar, 2023
Like Article
Save Article