Python - Concatenate string rows in Matrix
Last Updated :
09 Apr, 2023
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'], ['GeeksforGeeks']]
# 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'], ['GeeksforGeeks']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'GeeksforGeeks']
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'], ['GeeksforGeeks']]
# 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'], ['GeeksforGeeks']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'GeeksforGeeks']
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'], ['GeeksforGeeks']]
# 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
OutputThe original list : [['gfg', ' is', ' best'], ['Computer', ' Science'], ['GeeksforGeeks']]
The row concatenation in matrix : ['gfg is best', 'Computer Science', 'GeeksforGeeks']
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.
Similar Reads
Python - String Matrix Concatenation Sometimes, while working with Matrix we can have a problem in which we have Strings and we need a universal concatenation of all the String present in it. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + join() We can solve this problem using lis
4 min read
Python - Vertical Concatenation in Matrix 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"], ["i
3 min read
Python - Concatenate Ranged Values in String list Given list of strings, perform concatenation of ranged values from the Strings list. Input : test_list = ["abGFGcs", "cdforef", "asalloi"], i, j = 3, 5 Output : FGorll Explanation : All string sliced, FG, or and ll from all three strings and concatenated. Input : test_list = ["aGFGcs", "cforef", "aa
5 min read
Python - Concatenate Dictionary string values The task of concatenating string values in a Python dictionary involves combining the values associated with the same key across multiple dictionaries. This operation allows us to merge string data in an efficient manner, especially when dealing with large datasets .For example, given two dictionari
4 min read
Python - Concatenate Strings in the Given Order Given a String List and order list, perform string concatenation in a specific order. Input : test_list = ["best", "Gfg", "for", "is"], sort_order = [1, 3, 0, 2] Output : Gfgisbestfor Explanation : Combined as per order of indices. Input : test_list = ["best", "Gfg"], sort_order = [1, 0] Output : Gf
5 min read
Python - Convert Integer Matrix to String Matrix Given a matrix with integer values, convert each element to String. Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]] Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] Explanation : All elements of Matrix converted to Strings. Input : test_list = [[4, 5, 7], [10, 8, 3]] Output : [
6 min read