Friday, November 22, 2024
Google search engine
HomeLanguagesPython – Concatenate string rows in Matrix

Python – Concatenate string rows in Matrix

The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the concatenation of rows of matrix in uneven sized matrix. Let’s discuss certain ways in which this problem can be solved. 

Method #1 : Using join() + list comprehension The combination of above functions can help to get the solution to this particular problem in just a one line and hence quite useful. The join function computes the concatenation of sublists and all this bound together using list comprehension. 

Python3




# Python3 code to demonstrate
# Row String Concatenation Matrix
# using join() + list comprehension
 
# initializing list
test_list = [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
 
# printing original list
print("The original list : " + str(test_list))
 
# using join() + list comprehension
# Row String Concatenation Matrix
res = [''.join(idx for idx in sub) for sub in test_list ]
 
# print result
print("The row concatenation in matrix : " + str(res))


Output : 

The original list : [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'Lazyroar']

Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements

  Method #2 : Using loop This task can also be performed in brute force manner in which we just iterate the sublists and perform join in brute manner creating new string for each sublist and appending in list. 

Python3




# Python3 code to demonstrate
# Row String Concatenation Matrix
# using loop
 
# initializing list
test_list = [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
 
# printing original list
print("The original list : " + str(test_list))
 
# using loop
# Row String Concatenation Matrix
res = []
for sub in test_list:
    res_sub = ""
    for idx in sub:
        res_sub = res_sub + idx
    res.append(res_sub)
 
# print result
print("The row concatenation in matrix : " + str(res))


Output : 

The original list : [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'Lazyroar']

Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements

Approach 3 : Using map() function
This approach is similar to the method 2, but here we use map function instead of loop, as map can save time and has a cleaner implementation.

Python3




# Python3 code to demonstrate
# Row String Concatenation Matrix
# using map() function
   
# initializing list
test_list = [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
   
# printing original list
print("The original list : " + str(test_list))
   
# using map() function
# Row String Concatenation Matrix
res = list(map(lambda x : ''.join(x), test_list))
   
# print result
print("The row concatenation in matrix : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [['gfg', ' is', ' best'], ['Computer', ' Science'], ['Lazyroar']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'Lazyroar']

Time Complexity : O(N * M) , N is number of rows and M is length of longest sublist.

Auxiliary Space : O(N) , N is number of rows.

RELATED ARTICLES

Most Popular

Recent Comments