How to convert string tuples to list tuples in python? A string tuple is a tuple that is represented as a string, and a list tuple is a list of tuples. You can convert string tuples to list tuples in python using many ways, for example, by using eval() + list comprehension
, enumerate()
, regex
, map()+eval()
, and eval()
functions. In this article, I will explain all these methods with examples.
1. Quick Examples of Convert String Tuples to List Tuples
If you are in a hurry, below are some quick examples of how to convert string tuples to list tuples in python.
# Quick examples of convert string tuples to list tuples
# Example 1: Converting string tuples to list tuples
# Using list comprehension + eval()
string_tuples = ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
result = [eval(ele) for ele in string_tuples]
# Example 2: Convert string tuples to list tuples
# Using the enumerate() function
result = [eval(i) for a,i in enumerate(string_tuples)]
# Example 3: Using map() + eval() function
result = list(map(eval, string_tuples))
# Example 4: Using ast.literal_eval() instead of eval() function
result = [ast.literal_eval(ele) for ele in string_tuples]
# Example 5: Converting string tuples to list tuples
# Using regex
res = [tuple(map(int, re.findall(r'\d+', i))) if j.isdigit() else (j.strip("(')"), int(k)) for i in string_tuples for j, k in re.findall(r"\('(.*?)', (.*?)\)", i)]
2. Convert String Tuples to List Tuples Using eval() + List Comprehension
To convert a list of string tuples to a list of actual tuples, you can use the eval()
function along with a list comprehension.
For example, you first define a list string_tuples
that contains three string representations of tuples. you can use a list comprehension
to iterate over each string tuple in the input list string_tuples
. For each string tuple, you use the eval()
function to evaluate it as a Python expression and convert it into an actual tuple. The resulting list of tuples is stored in the variable result
.
# Initializing list
string_tuples = ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
print("Original String tuples: ",string_tuples)
# Converting string tuples to list tuples
# Using list comprehension + eval()
result = [eval(ele) for ele in string_tuples]
print("Updated list tuples : ", result)
Yields below output.
# Output:
# Original String tuples: ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
# Updated list tuples : [(2500, 'Python'), (2000, 'Hadoop'), (3000, 'Spark')]
3. Using the enumerate() Function
You can use the enumerate()
function to convert a string of tuples to a list of tuples. The enumerate()
function is a built-in Python function that allows you to iterate over a sequence (such as a list or tuple) and keep track of the current index of each element.
For example, you first define a list string_tuples
that contains three string tuples. You can use a list comprehension
and the enumerate()
function to loop over each string tuple in the string_tuples
list and convert it to an actual tuple using the eval()
function. The resulting list of tuples is stored in the result
variable.
# Initializing list
string_tuples = ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
print("Original String tuples: ",string_tuples)
# Convert string tuples to list tuples
# Using the enumerate() function
result = [eval(i) for a,i in enumerate(string_tuples)]
print("Updated list tuples : ", result)
Yields the same output as above.
4. Using map() & eval() to Convert
You can convert a string tuple to a list tuple using the map()
function and eval()
method.
For example, the eval()
evaluates the string expression inside each element of the list string_tuples
and returns a tuple for each element. map()
applies eval()
to each element string_tuples
, resulting in a map object containing the actual tuples. The list()
function is then used to convert the map object to a list.
# Initializing list
string_tuples = ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
print("Original String tuples: ",string_tuples)
# Using map() + eval() function
result = list(map(eval, string_tuples))
print("Updated list tuples : ", result)
Yields the same output as above.
5. Using ast.literal_eval() Instead of eval() Function
You can convert string tuples to list tuples using ast.literal_eval()
function. For example, list comprehension is used to apply ast.literal_eval()
to each element in string_tuples
. The resulting list of actual tuples is assigned to the result.
import ast
# Initializing list
string_tuples = ["(2500, 'Python')", "(2000, 'Hadoop')", "(3000, 'Spark')"]
print("Original String tuples: ",string_tuples)
# Using ast.literal_eval() instead of eval() function
result = [ast.literal_eval(ele) for ele in string_tuples]
print("Updated list tuples : ", result)
Yields the same output as above.
6. Convert String Tuples to List Tuples Using Regex Method
You can also convert string tuples to list tuples using regex
and list comprehensions. Here, the list comprehension contains two for loops and an if statement.
import re
string_tuples = ["('Python', 2500)", "('Hadoop', 2000)", "('Spark', 3000)"]
print("Original String tuples: ",string_tuples)
# Converting string tuples to list tuples
# Using regex
res = [tuple(map(int, re.findall(r'\d+', i))) if j.isdigit() else (j.strip("(')"), int(k)) for i in string_tuples for j, k in re.findall(r"\('(.*?)', (.*?)\)", i)]
print("Updated list tuples : ", res)
Yields the same output as above.
Conclusion
In this article, I have explained how to convert string tuples to list tuples in python by using eval() + list comprehension
, enumerate()
, regex
, map()+eval()
, and eval()
functions with examples.
Happy Learning !!
Related Articles
- Convert Tuple to String in Python
- Convert String to List of Tuples in Python
- Convert Tuple to List in Python
- Convert List to Tuple in Python
- Convert Tuples to Dictionary in Python
- Flatten List of Tuples to String in Python
- Convert List of Tuples into List in Python
- Convert List of Lists to Tuple of Tuples in Python
- Python Pad String With Zeros
- Python Catch Multiple Exceptions
- Python Return Tuple from Function
- Python Tuple index() Method