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.
Similar Reads
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using l
6 min read
Lists Of Strings In Python
A list of strings in Python stores multiple strings together. In this article, weâll explore how to create, modify and work with lists of strings using simple examples. Creating a List of StringsWe can use square brackets [] and separate each string with a comma to create a list of strings. [GFGTABS
2 min read
Python | Selective Merge in String list
Sometimes, while working with Python lists, we can have a problem in which we need to perform the merge operation. There can be various modifications to merge. One such can be replacing every character except a particular character. Let's see different ways in which this task can be performed. Metho
5 min read
Python | Merge Python key values to list
Sometimes, while working with Python, we might have a problem in which we need to get the values of dictionary from several dictionaries to be encapsulated into one dictionary. This type of problem can be common in domains in which we work with relational data like in web developments. Let's discuss
4 min read
Python Program to Convert a List to String
In Python, converting a list to a string is a common operation. In this article, we will explore the several methods to convert a list into a string. The most common method to convert a list of strings into a single string is by using join() method. Let's take an example about how to do it. Using th
3 min read
Python | List of tuples to String
Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed. Method #1: Using str() + str
8 min read
Python | Merge two lists into list of tuples
The task of merging two lists into a list of tuples involves combining corresponding elements from both lists into paired tuples. For example, given two lists like a = [1, 2, 3] and b = ['a', 'b', 'c'], the goal is to merge them into a list of tuples, resulting in [(1, 'a'), (2, 'b'), (3, 'c')]. Usi
3 min read
Convert String List to ASCII Values - Python
We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this. Using List Comprehension with
3 min read
Python | Convert String to list of tuples
Sometimes, while working with data, we can have a problem in which we have a string list of data and we need to convert the same to list of records. This kind of problem can come when we deal with a lot of string data. Let's discuss certain ways in which this task can be performed. Method #1: Using
8 min read
Python | Append String to list
Sometimes, while working with data, we can have a problem in which we need to add elements to a container. The list can contain any type of data type. Let's discuss certain ways in Python in which we can perform string append operations in the list of integers. Example: Append String at the end of a
5 min read