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 code to demonstrate working of # Convert String to tuple list # using loop + replace() + split() # initializing string test_str = "(1, 3, 4), (5, 6, 4), (1, 3, 6)" # printing original string print("The original string is : " + test_str) # Convert String to tuple list # using loop + replace() + split() res = [] temp = [] for token in test_str.split(", "): num = int(token.replace("(", "").replace(")", "")) temp.append(num) if ")" in token: res.append(tuple(temp)) temp = [] # printing result print("List after conversion from string : " + str(res)) |
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 code to demonstrate working of # Convert String to tuple list # using eval() # initializing string test_str = "(1, 3, 4), (5, 6, 4), (1, 3, 6)" # printing original string print("The original string is : " + test_str) # Convert String to tuple list # using eval() res = list(eval(test_str)) # printing result print("List after conversion from string : " + str(res)) |
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)]
Recommended Posts:
- Python - Convert Tuple Matrix to Tuple List
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Convert mixed data types tuple list to string list
- Python | Sort tuple list by Nth element of tuple
- Python - Flatten tuple of List to tuple
- Python | Convert list to indexed tuple list
- Python | Convert Integral list to tuple list
- Python | Convert List of Dictionary to Tuple list
- Python - Convert Tuple value list to List of tuples
- Python program to convert a list of strings with a delimiter to a list of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python | Convert List of String List to String List
- Python | Convert a list into a tuple
- Python | Convert a list into tuple of lists
- Python | Convert list of tuple into dictionary
- Python - Convert List of Lists to Tuple of Tuples
- Python - Convert List to Single valued Lists in Tuple
- Python | Merge Tuple String List values to String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

