Python | Ways to merge strings into list
Last Updated :
18 Apr, 2023
Given n strings, the task is to merge all strings into a single list. While developing an application, there come many scenarios when we need to operate on the string and convert it as some mutable data structure, say list. There are multiple ways we can convert strings into list based on the requirement. Let’s understand it better with help of examples.
Method #1: Using ast
Python3
import ast
str1 = " 'Geeks' , 'for' , 'Geeks' "
str2 = " 'paras.j' , 'jain.l' "
str3 = " 'india' "
list = []
for x in (str1, str2, str3):
list .extend(ast.literal_eval(x))
print ( list )
|
Output:
['Geeks', 'for', 'Geeks', 'paras.j', 'jain.l', 'i', 'n', 'd', 'i', 'a']
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method #2: Using eval
Python3
str1 = "[ 'Geeks' , 'for' , 'Geeks' ]"
str2 = "[ 'paras.j' , 'jain.l' ]"
str3 = "[ 'india' ]"
out = [str1, str2, str3]
out = eval ( '+' .join(out))
print (out)
|
Output:
['Geeks', 'for', 'Geeks', 'paras.j', 'jain.l', 'india']
Python3
str1 = " 'Geeks' , 'for' , 'Geeks' "
str2 = " '121' , '142' "
str3 = " 'extend' , 'India' "
out = [str1, str2, str3]
out = eval ( '+' .join(out))
print ( list (out))
|
Output:
['Geeks', 'for', 'Geeks121', '142extend', 'India']
Method #4: Using replace
Python3
str1 = "'Geeks', 'for', 'Geeks'"
str2 = "'paras.j', 'jain.l'"
str3 = "'india'"
list1 = [element.replace(
, " ") for element in str2.split(" ,")]
list3 = [element.replace( "'" , " ") for element in str3.split(" ,")]
merged_list = list1 + list2 + list3
print (merged_list)
|
Output
['Geeks', ' for', ' Geeks', 'paras.j', ' jain.l', 'india']
The code above is defining three strings, each representing a list of strings separated by a comma. The lists are then split by the comma character and the single quotes are removed from each element. Finally, the lists are merged into a single list.
To split the strings, the split() method is used. This method takes a delimiter as input and returns a list of the elements in the string, separated by that delimiter.
To remove the single quotes from the elements, the replace() method is used. This method takes two arguments: the string to be replaced, and the string to replace it with. In this case, the single quotes are replaced with an empty string, effectively removing them from the element.
The lists are then merged using the + operator.
The time complexity of this code is O(n), where n is the total number of elements in the lists. This is because each element in the lists is processed only once. The space complexity is also O(n), since a new list is created which is the size of the sum of the original lists.
Method#5: Using loop and split.
Algorithm:
- Initialize an empty list called “merged”.
- For each string in the input list “out”:
a. Remove the brackets from the string using string slicing (i.e. from index 1 to second last index).
b. Split the resulting string at comma and store the resulting list of words in a variable called “words”.
c. Extend the “merged” list with the “words” list.
- Print the “merged” list.
Python3
str1 = "['Geeks', 'for', 'Geeks']"
str2 = "['paras.j', 'jain.l']"
str3 = "['india']"
out = [str1, str2, str3]
merged = []
for s in out:
words = s[ 1 : - 1 ].split( ', ' )
merged.extend(words)
print (merged)
|
Output
["'Geeks'", "'for'", "'Geeks'", "'paras.j'", "'jain.l'", "'india'"]
Time complexity: O(nm), where “n” is the length of the input list “out” and “m” is the maximum length of the individual strings in “out”.
Auxiliary space complexity: O(nm), to store the “merged” list.
Share your thoughts in the comments
Please Login to comment...