Open In App

Python | Convert Character Matrix to single String

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Convert Character Matrix to single String
# Using join() + list comprehension
 
# initializing list
test_list = [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Character Matrix to single String
# Using join() + list comprehension
res = ''.join(ele for sub in test_list for ele in sub)
 
# printing result
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




# Python3 code to demonstrate working of
# Convert Character Matrix to single String
# Using join() + chain()
 
from itertools import chain
 
# Initializing list
test_list = [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Convert Character Matrix to single String
# Using join() + chain()
res = "".join(chain(*test_list))
 
# Printing result
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




# Python3 code to demonstrate working of
# Convert Character Matrix to single String
# Using sum() + map()
 
# initializing list
test_list = [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Character Matrix to single String
# Using sum() + map()
res = ''.join(sum(map(list, test_list), []))
 
# printing result
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




# Python3 code to demonstrate working of
# Convert Character Matrix to single String
# Using reduce()
 
from functools import reduce
 
# initializing list
test_list = [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert Character Matrix to single String
# Using reduce()
res = reduce(lambda x, y: x+y, [char for row in test_list for char in row])
 
# printing result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads