Python | Convert List of lists to list of Strings
Last Updated :
06 Apr, 2023
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
# Python3 code to demonstrate working of
# Convert List of lists to list of Strings
# using list comprehension + join()
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings
# using list comprehension + join()
res = [''.join(ele) for ele in test_list]
# printing result
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
# Python3 code to demonstrate working of
# Convert List of lists to list of Strings
# using map() + join()
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings
# using map() + join()
res = list(map(''.join, test_list))
# printing result
print("The String of list is : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Convert List of lists to list of Strings
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings
res = []
for i in test_list:
s = ""
for j in i:
s += j
res.append(s)
# printing result
print("The String of list is : " + str(res))
OutputThe 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
# initialize list
test_list = [["g", "f", "g"], ["i", "s"], ["b", "e", "s", "t"]]
# printing original list
print("The original list : " + str(test_list))
# Convert List of lists to list of Strings using reduce()
res = [reduce(lambda x, y: x + y, inner_list) for inner_list in test_list]
# printing result
print("The String of list is : " + str(res))
OutputThe 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.
Similar Reads
Python | Convert List of String List to String List Sometimes while working in Python, we can have problems of the interconversion of data. This article talks about the conversion of list of List Strings to joined string list. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + generator expression + join() + isd
6 min read
Python - Convert List of Integers to a List of Strings We are given a list of integers and our task is to convert each integer into its string representation. For example, if we have a list like [1, 2, 3] then the output should be ['1', '2', '3']. In Python, there are multiple ways to do this efficiently, some of them are: using functions like map(), re
3 min read
Convert list of strings to list of tuples in Python Sometimes we deal with different types of data types and we require to inter-convert from one data type to another hence interconversion is always a useful tool to have knowledge. This article deals with the converse case. Let's discuss certain ways in which this can be done in Python. Method 1: Con
5 min read
Convert List of Tuples to List of Strings - Python The task is to convert a list of tuples where each tuple contains individual characters, into a list of strings by concatenating the characters in each tuple. This involves taking each tuple, joining its elements into a single string, and creating a new list containing these strings.For example, giv
3 min read
Python | Convert list of tuples to list of list Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples.Using numpyNumPy makes it easy to convert a list of tu
3 min read
Python | Convert string enclosed list to list Given a list enclosed within a string (or quotes), write a Python program to convert the given string to list type. Examples: Input : "[0, 2, 9, 4, 8]" Output : [0, 2, 9, 4, 8] Input : "['x', 'y', 'z']" Output : ['x', 'y', 'z'] Approach #1: Python eval() The eval() method parses the expression passe
5 min read
Python | Convert list of numerical string to list of Integers Many times, the data we handle might not be in the desired form for any application and has to go through the stage of preprocessing. One such kind of form can be a number in the form of a string that too is a list in the list and we need to segregate it into digit-separated integers. Let's discuss
5 min read
Python - List of float to string conversion When working with lists of floats in Python, we may often need to convert the elements of the list from float to string format. For example, if we have a list of floating-point numbers like [1.23, 4.56, 7.89], converting them to strings allows us to perform string-specific operations or output them
3 min read
Convert Set of Tuples to a List of Lists in Python Sets and lists are two basic data structures in programming that have distinct uses. It is sometimes necessary to transform a collection of tuples into a list of lists. Each tuple is converted into a list throughout this procedure, and these lists are subsequently compiled into a single, bigger list
3 min read
Python - Convert String to List of dictionaries Given List of dictionaries in String format, Convert into actual List of Dictionaries. Input : test_str = ["[{'Gfg' : 3, 'Best' : 8}, {'Gfg' : 4, 'Best' : 8}]"] Output : [[{'Gfg': 3, 'Best': 8}, {'Gfg': 4, 'Best': 8}]] Explanation : String converted to list of dictionaries. Input : test_str = ["[{'G
4 min read