Python | Convert String to tuple list
Last Updated :
07 Apr, 2023
Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop + split() + replace() This is a brute force method to perform this task. In this, we perform the task of extracting and remaking the tuples to list in a loop using split() and replace() functionalities.
Python3
test_str = "( 1 , 3 , 4 ), ( 5 , 6 , 4 ), ( 1 , 3 , 6 )"
print ("The original string is : " + test_str)
res = []
temp = []
for token in test_str.split(", "):
num = int (token.replace("(", "").replace(")", ""))
temp.append(num)
if ")" in token:
res.append( tuple (temp))
temp = []
print (" List after conversion from string : " + str (res))
|
Output :
The original string is : (1, 3, 4), (5, 6, 4), (1, 3, 6)
List after conversion from string : [(1, 3, 4), (5, 6, 4), (1, 3, 6)]
Method #2: Using eval() This inbuilt function can also be used to perform this task. This function internally evaluates the string and returns the converted list of tuples as desired.
Python3
test_str = "( 1 , 3 , 4 ), ( 5 , 6 , 4 ), ( 1 , 3 , 6 )"
print ("The original string is : " + test_str)
res = list ( eval (test_str))
print (" List after conversion from string : " + str (res))
|
Output :
The original string is : (1, 3, 4), (5, 6, 4), (1, 3, 6)
List after conversion from string : [(1, 3, 4), (5, 6, 4), (1, 3, 6)]
Method #3: Using re.findall() and ast.literal_eval()
Another way to convert string to tuple list is by using the findall() function from the re module to find all the tuples and then use the literal_eval() function from the ast module to convert the string into a tuple.
Python3
import re
import ast
test_str = "(1, 3, 4), (5, 6, 4), (1, 3, 6)"
print ( "The original string is : " + test_str)
res = [ast.literal_eval(i) for i in re.findall(r '\(.*?\)' , test_str)]
print ( "List after conversion from string : " + str (res))
|
Output
The original string is : (1, 3, 4), (5, 6, 4), (1, 3, 6)
List after conversion from string : [(1, 3, 4), (5, 6, 4), (1, 3, 6)]
In this method, first, we use the findall() function from the re module to find all the tuples that are present in the given string, it returns a list of tuples in string format. Then we use the literal_eval() function from the ast module to convert those strings into actual tuples. The time complexity of this approach is O(n) where n is the number of tuples present in the string, space complexity is also O(n).
Method #4: Using list comprehension + tuple()
Step-by-step explanation:
- We start by initializing the test string.
- We use the split() function to split the string into a list of tokens at every “, “.
- We then use a list comprehension to iterate over each token in the list and create a tuple from it.
- We use the map() function to apply the int() function to each item in the token, which converts them from strings to integers.
- We use replace() to remove the opening and closing parenthesis from each token.
- We split each token at every “, ” using split().
- We wrap the resulting list of integers in the tuple() function to create a tuple.
- We append each tuple to a new list called res.
- Finally, we print out the result
Python3
test_str = "(1, 3, 4), (5, 6, 4), (1, 3, 6)"
print ( "The original string is : " + test_str)
res = [ tuple ( map ( int , t.replace( '(' , ' ').replace(' ) ', ' ').split(' , '))) for t in test_str.split(' , ')]
print ( "List after conversion from string : " + str (res))
|
Output
The original string is : (1, 3, 4), (5, 6, 4), (1, 3, 6)
List after conversion from string : [(1,), (3,), (4,), (5,), (6,), (4,), (1,), (3,), (6,)]
Time complexity: O(n), where n is the number of tokens in the input string.
Auxiliary space: O(n), where n is the number of tokens in the input string. This is because we create a new list of tuples to store the result.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...