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 functionalities can be used to perform this task. In this, we iterate through list using list comprehension and can perform split using split().
Python3
# Python3 code to demonstrate working of # Convert String List to Nested Character List # using split() + list comprehension # initialize list test_list = ["a, b, c", "gfg, is , best", " 1 , 2 , 3 "] # printing original list print ("The original list : " + str (test_list)) # Convert String List to Nested Character List # using split() + list comprehension res = [char.split( ', ' ) for char in test_list] # printing result print (" List after performing conversion : " + str (res)) |
The original list : [‘a, b, c’, ‘gfg, is, best’, ‘1, 2, 3’] List after performing conversion : [[‘a’, ‘b’, ‘c’], [‘gfg’, ‘is’, ‘best’], [‘1’, ‘2’, ‘3’]]
Time Complexity: O(n*n) where n is the number of elements in the string list. The list comprehension + split() is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using map() + split() + lambda The combination of above functions can be used to perform this task. In this, we perform the task of iteration using map() and lambda function is used to apply the logic of split using split() to all the list elements.
step-by-step approach
- Initialize the input list test_list with three string elements containing comma-separated values.
- Print the original list for verification.
- Use the map() function to apply a lambda function to each element of test_list.
- The lambda function splits each element of test_list into a list of strings using the delimiter ‘, ‘.
- The resulting list of lists is stored in the variable res.
- Print the resulting list of lists for verification.
Python3
# Python3 code to demonstrate working of # Convert String List to Nested Character List # using map() + split() + lambda # initialize list test_list = ["a, b, c", "gfg, is , best", " 1 , 2 , 3 "] # printing original list print ("The original list : " + str (test_list)) # Convert String List to Nested Character List # using map() + split() + lambda res = list ( map ( lambda ele: ele.split( ', ' ), test_list)) # printing result print (" List after performing conversion : " + str (res)) |
The original list : [‘a, b, c’, ‘gfg, is, best’, ‘1, 2, 3’] List after performing conversion : [[‘a’, ‘b’, ‘c’], [‘gfg’, ‘is’, ‘best’], [‘1’, ‘2’, ‘3’]]
Time Complexity: O(n) where n is the number of elements in the string list. The map() + split() + lambda is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res test_list.
Method #3 : Using regex
Python3
# Python3 code to demonstrate working of # Convert String List to Nested Character List # using re.split() # importing regular expression import re # initialize list test_list = [ "a, b, c" , "gfg, is, best" , "1, 2, 3" ] # printing original list print ( "The original list : " + str (test_list)) # Convert String List to Nested Character List # using re.split() res = [re.split( ', ' , ele) for ele in test_list] # printing result print ( "List after performing conversion : " + str (res)) |
The original list : ['a, b, c', 'gfg, is, best', '1, 2, 3'] List after performing conversion : [['a', 'b', 'c'], ['gfg', 'is', 'best'], ['1', '2', '3']]
Time complexity: O(n*m)
Auxiliary Space : O(n*m)
Method#4: Using Recursive method.
Algorithm:
- Define a function str_to_nested_list that takes a list lst as input.
- Base case: If the input list lst is empty, return an empty list.
- Split the first element of the list lst using the split() function to create a nested list of characters.
- Return the nested list concatenated with the result of calling the function str_to_nested_list with the rest of the input list lst[1:].
- Print the resulting nested list.
Python3
def str_to_nested_list(lst): if not lst: return [] char_lst = lst[ 0 ].split( ', ' ) return [char_lst] + str_to_nested_list(lst[ 1 :]) # initialize list test_list = [ "a, b, c" , "gfg, is, best" , "1, 2, 3" ] # printing original list print ( "The original list : " + str (test_list)) # Convert String List to Nested Character List # using re.split() res = str_to_nested_list(test_list) # printing result print ( "List after performing conversion : " + str (res)) |
The original list : ['a, b, c', 'gfg, is, best', '1, 2, 3'] List after performing conversion : [['a', 'b', 'c'], ['gfg', 'is', 'best'], ['1', '2', '3']]
Time complexity: O(n * m), where n is the length of the input list lst and m is the length of the longest string in the list.
Auxiliary Space: O(n * m), since we are creating a nested list with n sublists, and each sublist may have m elements.
Method#5: Using enumeration
Algorithm:
- Initialize an empty list res.
- Loop through each element of the input list test_list.
- Split the element by comma and space using split() method.
- Initialize an empty list inside res for each element.
- Loop through each character of the split element and append it to the inner list.
- Append the inner list to the main list res.
- Return the final list res.
Python3
# Python3 code to demonstrate working of # Convert String List to Nested Character List # using enumeration # initialize list test_list = [ "a, b, c" , "gfg, is, best" , "1, 2, 3" ] # printing original list print ( "The original list : " + str (test_list)) # Convert String List to Nested Character List # using enumeration res = [] for i, char in enumerate (test_list): res.append([]) for c in char.split( ', ' ): res[i].append(c) # printing result print ( "List after performing conversion : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original list : ['a, b, c', 'gfg, is, best', '1, 2, 3'] List after performing conversion : [['a', 'b', 'c'], ['gfg', 'is', 'best'], ['1', '2', '3']]
Time Complexity: O(n*m)
n is the length of the input list, test_list
m is the maximum length of a string in test_list (i.e. the maximum number of characters to be split)
The algorithm loops through each element of test_list and loops through each character in the split string, giving a time complexity of O(n*m).
Space Complexity: O(n*m)
The space required to store the final list res is O(n*m).
This is because the algorithm creates a new list inside res for each string in test_list and appends the characters to it. The size of each inner list is proportional to the length of the string, so the total space required is O(n*m).
Method#6: Using replace() and split() method
- Initialize the input list original_list
- Printing the original list.
- Using a list comprehension to create a new list.
- For each elem, using the replace() method to replace ‘,’ with an empty string ”.
- Using the split() method to split the resulting string.
- Appending the new list to the nested_list.
- Printing the output
Python3
# initialize list original_list = [ 'a, b, c' , 'gfg, is, best' , '1, 2, 3' ] # printing original list print ( "The original list : " + str (original_list)) # Convert String List to Nested Character List using replace() and split() nested_list = [elem.replace( ',' , '').split() for elem in original_list] # printing result print ( "List after performing conversion : " + str (nested_list)) |
The original list : ['a, b, c', 'gfg, is, best', '1, 2, 3'] List after performing conversion : [['a', 'b', 'c'], ['gfg', 'is', 'best'], ['1', '2', '3']]
Time Complexity: O(N*M) where n is the length of the input list and m is the length of the longest string in the input list.
Space Complexity: O(N*M) as we need to store each character in the output list.