I have the following issue with Python and I was wondering if there is a direct way to deal with it (using a specific function which I am not aware of, etc.).
I have a list with different types of inputs (integer, string, etc.). I transform the list into a string using the str()
function. Now the type of the variable is a string. I do some processing with this string and then I want to transform the string back into the initial list with the initial variable types it had (integer, string, etc.).
Here is an illustration:
list = [1,'house',3]
print(type(list)) # gives <class list>
print(type(list[0])) # gives <type int>
print(type(list[1])) # gives <type str>
string = str(list)
print(type(string)) # gives <type string>
... # use this string to process data
# convert the string into the initial list
? ?
I thought maybe I could in the beginning iterate my list and store the type of its attributes in a list (list_b
). Later when I convert the string to a list I will explode the string and convert the strings to the variable types corresponding to the list_b
.
I was wondering if there is a more straight forward way than this?