Python | Convert Character Matrix to single String
Last Updated :
26 Apr, 2023
Sometimes, while working with Python strings, we can have an option in which we need to perform the task of converting a character matrix to a single string. This can have applications in domains in which we need to work with data. Let us discuss certain ways in which we can perform this task.
Method #1 : Using join() + list comprehension
The combination of the above functionalities can be used to perform this task. In this, we just iterate for all lists and join them using join().
Python3
test_list = [[ 'g' , 'f' , 'g' ], [ 'i' , 's' ], [ 'b' , 'e' , 's' , 't' ]]
print ( "The original list is : " + str (test_list))
res = ''.join(ele for sub in test_list for ele in sub)
print ( "The String after join : " + res)
|
Output :
The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String after join : gfgisbest
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the join() + list comprehension which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2: Using join() + chain()
The combination of the above functionalities can be used to perform this task. In this, we perform the task performed by list comprehension by chain() method.
Python3
from itertools import chain
test_list = [[ 'g' , 'f' , 'g' ], [ 'i' , 's' ], [ 'b' , 'e' , 's' , 't' ]]
print ( "The original list is : " + str (test_list))
res = "".join(chain( * test_list))
print ( "The String after join : " + res)
|
Output :
The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String after join : gfgisbest
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the join() + chain() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #3 : Here is another approach using sum and map:
Python3
test_list = [[ 'g' , 'f' , 'g' ], [ 'i' , 's' ], [ 'b' , 'e' , 's' , 't' ]]
print ( "The original list is : " + str (test_list))
res = ''.join( sum ( map ( list , test_list), []))
print ( "The String after join : " + res)
|
Output
The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String after join : gfgisbest
Time complexity: O(n) where n is the total number of elements in the list.
Auxiliary Space: O(n) where n is the total number of elements in the list.
Explanation: The map function maps the lists in the input list to lists and the sum function sums these lists and converts the list of lists to a single list. The join function is used to join all the elements in the list to form a single string.
Method 4 : using the reduce() function from the functools module:
Python3
from functools import reduce
test_list = [[ 'g' , 'f' , 'g' ], [ 'i' , 's' ], [ 'b' , 'e' , 's' , 't' ]]
print ( "The original list is : " + str (test_list))
res = reduce ( lambda x, y: x + y, [char for row in test_list for char in row])
print ( "The String after join : " + res)
|
Output
The original list is : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String after join : gfgisbest
Time complexity: O(n^2) since we need to iterate over each character in the matrix.
Auxiliary Space: O(n) since we create a new list of characters using a list comprehension.
Please Login to comment...