DDM
DDM

Reputation: 323

Remove outer double quotation marks from a list of tuples

I have a list called data which I converted from pandas Dataframe with this syntax df.annotations.values.tolist() and I am trying to remove the outer double quotations from it which is a list of tuples. Is there any simple way I could do it?

From

["('Jack', {'entities': [(0, 12, 'PERSON')]})",
 "('Alba', {'entities': [(0, 15, 'PERSON')]})",
 "('Brandy at town', {'entities': [(0, 15, 'PERSON')]})",
 "('Hailey', {'entities': [(0, 10, 'PERSON')]})"]

to

[('Jack', {'entities': [(0, 12, 'PERSON')]}),
 ('Alba', {'entities': [(0, 15, 'PERSON')]}),
 ('Brandy at town', {'entities': [(0, 15, 'PERSON')]}),
 ('Hailey', {'entities': [(0, 10, 'PERSON')]})]

Upvotes: 0

Views: 87

Answers (1)

user7864386
user7864386

Reputation:

You can use ast.literal_eval:

import ast
out = list(map(ast.literal_eval, lst))

as a list comprehension:

out = [ast.literal_eval(x) for x in lst]

Output:

[('Jack', {'entities': [(0, 12, 'PERSON')]}),
 ('Alba', {'entities': [(0, 15, 'PERSON')]}),
 ('Brandy at town', {'entities': [(0, 15, 'PERSON')]}),
 ('Hailey', {'entities': [(0, 10, 'PERSON')]})]

Upvotes: 2

Related Questions