I have the following list:
a= ["('A442', 'A563')", "('A545', 'A779')"]
I need to get a tuple like this:
a = [('A442', 'A563'), ('A545', 'A779')]
I tried to use replace to replace the quotes, but it didn't work for me
a= [x.replace('""', '') for x in a]
Can somebody help me. Thank you!
The simplest but most dangerous method would be
eval()
:But the use of
eval()
is very dangerous, since it can execute malicious code.A safer alternative would be.
If we improve it a bit we could use:
In this way, it no longer requires a space between each item of the tuple.
And in case you don't mind importing modules you can use
re.sub
to replace the.replace
: