James
James

Reputation: 15

how to convert a list of strings into a list of variables?

Hi might be a simple question. How do i convert this:

mylist = ['table[0].df', 'table[1].df']

to this

mylist = [table[0].df, table[1].df]

Thank you.

Upvotes: 1

Views: 399

Answers (2)

Mouad Slimane
Mouad Slimane

Reputation: 1067

try this:

new_list=list(map(lambda x:eval(x),my_list))

Upvotes: 1

T C Molenaar
T C Molenaar

Reputation: 3260

Here is a possible solution using eval. For every item in the list, eval() is used and saved on the same location as the original string.

mylist = ['table[0].df', 'table[1].df']

for i, item in enumerate(mylist):
    mylist[i] = eval(item)

Upvotes: 1

Related Questions