Python | Convert string tuples to list tuples
Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to list of tuples. This kind of problem can have it’s occurrence in data science domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using eval() + list comprehension
This problem can be easily performed as one liner using the inbuilt function of eval(), which performs this task of string to tuple conversion and list comprehension.
# Python3 code to demonstrate working of # Converting string tuples to list tuples # using list comprehension + eval() # Initializing list test_list = ["('gfg', 1)", "('is', 2)", "('best', 3)"] # printing original list print("The original list is : " + str(test_list)) # Converting string tuples to list tuples # using list comprehension + eval() res = [eval(ele) for ele in test_list] # printing result print("The list tuple after conversion : " + str(res)) |
The original list is : ["('gfg', 1)", "('is', 2)", "('best', 3)"]
The list tuple after conversion : [('gfg', 1), ('is', 2), ('best', 3)]
Method #2 : Using eval() + map()
This task can also be performed using combination of above functions. The task performed by list comprehension above can be performed using map() in this method.
# Python3 code to demonstrate working of # Converting string tuples to list tuples # using map() + eval() # Initializing list test_list = ["('gfg', 1)", "('is', 2)", "('best', 3)"] # printing original list print("The original list is : " + str(test_list)) # Converting string tuples to list tuples # using map() + eval() res = list(map(eval, test_list)) # printing result print("The list tuple after conversion : " + str(res)) |
The original list is : ["('gfg', 1)", "('is', 2)", "('best', 3)"]
The list tuple after conversion : [('gfg', 1), ('is', 2), ('best', 3)]
Recommended Posts:
- Python | Convert String to list of tuples
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Count tuples occurrence in list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Combining tuples in list of tuples
- Python | Convert list elements to bi-tuples
- Python | Convert list of tuples into digits
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Convert list of tuples to list of strings
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples into list
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.



