Python - String Matrix Concatenation
Last Updated :
13 Apr, 2023
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 list comprehension as a potential shorthand to the conventional loops that we may use to perform this particular task. We just join the elements extracted and put them into a as a single string.
Python3
# Python3 code to demonstrate
# String Matrix Concatenation
# Using list comprehension
# initializing list
test_list = [["geeksforgeeks", " is", " best"], [" I", " Love"], [" Gfg"]]
# printing original list
print("The original list : " + str(test_list))
# using list comprehension
# count of all the elements in list
res = "".join([ele for sub in test_list for ele in sub])
# print result
print("The Matrix Concatenation is : " + str(res))
OutputThe original list : [['geeksforgeeks', ' is', ' best'], [' I', ' Love'], [' Gfg']]
The Matrix Concatenation is : geeksforgeeks is best I Love Gfg
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 list of m * n elements
Method #2: Using chain() + join() This particular problem can also be solved using the chain function instead of list comprehension in which we use the conventional join function to join.
Python3
# Python3 code to demonstrate
# String Matrix Concatenation
# Using chain() + join()
from itertools import chain
# initializing list
test_list = [["geeksforgeeks", " is", " best"], [" I", " Love"], [" Gfg"]]
# printing original list
print("The original list : " + str(test_list))
# using chain() + join()
# String Matrix Concatenation
res = "".join(list(chain(*test_list)))
# print result
print("The Matrix Concatenation is : " + str(res))
OutputThe original list : [['geeksforgeeks', ' is', ' best'], [' I', ' Love'], [' Gfg']]
The Matrix Concatenation is : geeksforgeeks is best I Love Gfg
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 list of m * n elements
Method #3 : Using extend() and join() methods
Python3
# Python3 code to demonstrate
# String Matrix Concatenation
# initializing list
test_list = [["geeksforgeeks", " is", " best"], [" I", " Love"], [" Gfg"]]
# printing original list
print("The original list : " + str(test_list))
res=[]
for i in test_list:
res.extend(i)
res=" ".join(res)
# print result
print("The Matrix Concatenation is : " + str(res))
OutputThe original list : [['geeksforgeeks', ' is', ' best'], [' I', ' Love'], [' Gfg']]
The Matrix Concatenation is : geeksforgeeks is best I Love Gfg
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #4 : Using reduce()
Here, reduce() function applies the lambda function to the items of the list in a cumulative way, such that the first item is passed as the first argument, the second item as the second argument and so on. In this case, the lambda function takes two arguments x and y and concatenates them.
Python3
from functools import reduce
# initializing list
test_list = [["geeksforgeeks", " is", " best"], [" I", " Love"], [" Gfg"]]
# printing original list
print("The original list : " + str(test_list))
# using reduce()
result = reduce(lambda x, y: x+y, reduce(lambda x, y: x+y, test_list))
# print result
print("The Matrix Concatenation is : " + str(result))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [['geeksforgeeks', ' is', ' best'], [' I', ' Love'], [' Gfg']]
The Matrix Concatenation is : geeksforgeeks is best I Love Gfg
Time complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
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 joi
3 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 - Combine Strings to Matrix Sometimes while working with data, we can receive separate data in the form of strings and we need to compile them into Matrix for its further use. This can have applications in many domains. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + split
4 min read
Python - Alternate Strings Concatenation The problem of getting the concatenation of a list is quite generic and we might someday face the issue of getting the concatenation of alternate elements and get the list of 2 elements containing the concatenation of alternate elements. Letâs discuss certain ways in which this can be performed. Met
3 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 - Triple quote String concatenation Sometimes, while working with Python Strings, we can have a problem in which we need to perform concatenation of Strings which are constructed by Triple quotes. This happens in cases we have multiline strings. This can have applications in many domains. Let us discuss certain ways in which this task
4 min read