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.



Similar Reads

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 : [['4', '5', '7'], ['10', '8', '3']] Explanation : A
6 min read
Python Program to Convert String Matrix Representation to Matrix
Given a String with matrix representation, the task here is to write a python program that converts it to a matrix. Input : test_str = "[gfg,is],[best,for],[all,geeks]"Output : [['gfg', 'is'], ['best', 'for'], ['all', 'geeks']]Explanation : Required String Matrix is converted to Matrix with list as data type. Input : test_str = "[gfg,is],[for],[all
4 min read
Python - Convert Strings to Character Matrix
Sometimes, while dealing with String lists, we can have a problem in which we need to convert the Strings in list to separate character list. Overall converting into Matrix. This can have multiple applications in data science domain in which we deal with lots of data. Lets discuss certain ways in which this task can be performed. Method #1 : Using
3 min read
Python | Convert tuple records to single string
Sometimes, while working with data, we can have a problem in which we have tuple records and we need to change it's to comma-separated strings. These can be data regarding names. This kind of problem has its application in the web development domain. Let's discuss certain ways in which this problem can be solved Method #1: Using join() + list compr
6 min read
Python | Convert String list to Joined Single element
Sometimes, while working with Python, we can have a problem in which we need to perform the task of joining each element of String list to a single element in List by combining using delim. This kind of application can come in web development domain. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop This is one o
4 min read
Python | Replace multiple occurrence of character by single
Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character. Examples: Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : WinReplace multiple occurrence of character by singleApproach #1 : Naive Approach This method is a brute force approach
4 min read
How to Take Only a Single Character as an Input in Python
Through this article, you will learn how to accept only one character as input from the user in Python. Prompting the user again and again for a single character To accept only a single character from the user input: Run a while loop to iterate until and unless the user inputs a single character.If the user inputs a single character, then break out
3 min read
Python | Convert string List to Nested Character List
Sometimes, while working with Python, we can have a problem in which we need to perform interconversion of data. In this article we discuss converting String list to Nested Character list split by comma. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + split() The combination of above functional
7 min read
Python - Convert Alternate String Character to Integer
Interconversion between data types is facilitated by python libraries quite easily. But the problem of converting the alternate list of string to integers is quite common in development domain. Let’s discuss few ways to solve this particular problem. Method #1 : Naive Method This is most generic method that strikes any programmer while performing t
5 min read
Python program to Convert a Matrix to Sparse Matrix
Given a matrix with most of its elements as 0, we need to convert this matrix into a sparse matrix in Python. Example: Input: Matrix: 1 0 0 0 0 2 0 0 0 0 3 0 0 0 0 4 5 0 0 0 Output: Sparse Matrix: 0 0 1 1 1 2 2 2 3 3 3 4 4 0 5 Explanation: Here the Matrix is represented using a 2D list and the Sparse Matrix is represented in the form Row Column Val
3 min read
three90RightbarBannerImg