Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + join()
The combination of above functionalities can be used to perform this task. In this, we perform the task of iteration using list comprehension and join() is used to perform task of joining string to list of strings.
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = [''.join(ele) for ele in test_list]
print ( "The String of list is : " + str (res))
|
Output :
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #2: Using map() + join()
The task above can also be performed using combination of above methods. In this, we perform the task of conversion using join and iteration using map().
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = list ( map (''.join, test_list))
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Without any builtin methods
Python3
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
s = ""
for j in i:
s + = j
res.append(s)
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #4: Using reduce() function
Use the reduce() function from the functools module to join all the strings in the inner lists
- Initialize list
- printing original list
- Convert List of lists to list of Strings using reduce()
- printing result
Python3
from functools import reduce
test_list = [[ "g" , "f" , "g" ], [ "i" , "s" ], [ "b" , "e" , "s" , "t" ]]
print ( "The original list : " + str (test_list))
res = [ reduce ( lambda x, y: x + y, inner_list) for inner_list in test_list]
print ( "The String of list is : " + str (res))
|
Output
The original list : [['g', 'f', 'g'], ['i', 's'], ['b', 'e', 's', 't']]
The String of list is : ['gfg', 'is', 'best']
Time complexity: O(n*m), where n is the length of the outer list and m is the length of the longest inner list.
Auxiliary space: O(n), where n is the length of the outer list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!