Open In App

Python Program to Convert Matrix to String

Last Updated : 08 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a matrix, our task is to write a Python program to convert to the matrix, with different delimiters for element and row separation.

Examples:

Input : test_list = test_list = [[1, 3, “gfg”], [2, “is”, 4], [“best”, 9, 5]], in_del, out_del = “,”, ” “

Output : 1,3,gfg 2,is,4 best,9,5

Explanation : Element in list separated by “,”, and lists separated by ” “.

Input : test_list = test_list = [[1, 3, “gfg”], [2, “is”, 4], [“best”, 9, 5]], in_del, out_del = “,”, “-“

Output : 1,3,gfg-2,is,4-best,9,5

Explanation : Element in list separated by “,”, and lists separated by “-“.

Method #1 : Using join() + list comprehension

In this, we perform task of iterating each element of each row using list comprehension. The inner and outer join for element and row with different delimiters using join().

Python3




# Python3 code to demonstrate working of
# Convert Matrix to String
# Using list comprehension + join()
 
# initializing list
test_list = [[1, 3, "gfg"], [2, "is", 4], ["best", 9, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delims
in_del, out_del = ",", " "
 
# nested join using join()
res = out_del.join([in_del.join([str(ele) for ele in sub]) for sub in test_list])
 
# printing result
print("Conversion to String : " + str(res))


Output:

The original list is : [[1, 3, ‘gfg’], [2, ‘is’, 4], [‘best’, 9, 5]]

Conversion to String : 1,3,gfg 2,is,4 best,9,5

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”. The join() + list comprehension is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n), new list of size O(n) is created where n is the number of elements in the list

Method #2 : Using map() + join()

In this, task of inner joining of elements is extending to each character using map(). Rest all the functionalities are similar to upper method.

Python3




# Python3 code to demonstrate working of
# Convert Matrix to String
# Using map() + join()
 
# initializing list
test_list = [[1, 3, "gfg"], [2, "is", 4], ["best", 9, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delims
in_del, out_del = ",", " "
 
# nested join using join()
# map() for joining inner elements
res = out_del.join(in_del.join(map(str, sub)) for sub in test_list)
 
# printing result
print("Conversion to String : " + str(res))


Output:

The original list is : [[1, 3, ‘gfg’], [2, ‘is’, 4], [‘best’, 9, 5]]

Conversion to String : 1,3,gfg 2,is,4 best,9,5

The time and space complexity for all the methods are the same:

Time Complexity: O(n)

Auxiliary Space: O(n)

Approach 3: Using reduce() function and join() method

Python3




# Python3 code to demonstrate working of
# Convert Matrix to String
# Using reduce() + join()
 
# Importing reduce from functools module
from functools import reduce
 
# initializing list
test_list = [[1, 3, "gfg"], [2, "is", 4], ["best", 9, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delims
in_del, out_del = ",", " "
 
# Using reduce to join elements of sublists
res = out_del.join(reduce(lambda x, y: x + [in_del.join(map(str, y))], test_list, []))
 
# printing result
print("Conversion to String : " + str(res))


Output

The original list is : [[1, 3, 'gfg'], [2, 'is', 4], ['best', 9, 5]]
Conversion to String : 1,3,gfg 2,is,4 best,9,5

Explanation:

In this approach, the reduce() function is used to join the elements of the sublists in the matrix. The function takes the matrix test_list as an input and uses the reduce() function to join the elements of the sublists in the matrix. The lambda function takes two parameters x and y and returns the result of joining the elements of y using the in_del separator. The result is then joined using the out_del separator using the join() method.

Time Complexity: O(n)
Auxiliary Space: O(n)

Approach 4 : Using itertools.chain and str.join

 step-by-step algorithm for the approach 

  1. Import the itertools module.
  2. Print the original input matrix test_list.
  3. Initialize the input and output delimiters in_del and out_del.
  4. Use a list comprehension to loop over each inner list in the matrix test_list.
    a. Convert each element in the inner list to a string using the str() function.
    b. Use in_del.join() to join the stringified elements in the inner list with the input delimiter in_del.
  5. Use itertools.chain() to flatten the list of joined strings into a single list of strings.
  6. Use str.join() to join the list of strings with the output delimiter out_del.
  7. Store the resulting string in the variable res.
  8. Print the resulting string res.

Python3




# importing required module
import itertools
 
# initializing list
test_list = [[1, 3, "gfg"], [2, "is", 4], ["best", 9, 5]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing delims
in_del, out_del = ",", " "
 
# using itertools.chain and str.join to join elements
res = out_del.join([in_del.join(map(str, row)) for row in test_list])
 
# printing result
print("Conversion to String : " + str(res))


Output

The original list is : [[1, 3, 'gfg'], [2, 'is', 4], ['best', 9, 5]]
Conversion to String : 1,3,gfg 2,is,4 best,9,5

Time complexity of this approach is O(nm), where n is the number of rows in the input matrix and m is the number of columns in the input matrix. This is because the list comprehension loops over each inner list in the matrix and the join() operations also loop over each element in each inner list. 

Auxiliary space is also O(nm), as a new list is created to store the joined elements, and a new string is created to store the final result. However, the itertools.chain() function is implemented as a generator, which means it doesn’t create a new list in memory, making it more memory-efficient than some other approaches. Overall, this approach is relatively efficient and concise.



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

Similar Reads