Remove quotes from a list of tuples in python

Advertisements

Hoping someone can help me to figure out how to exclude quotes from a list of tuples and display it in the final output. I’ve tried regex a nd f-string format but nothing seem to work 🙁

lts = [('A', 1), ('B', 14), ('C', 12), ('D', 11), ('E', 9)]
Regex - [(re.sub(r"[^a-zA-Z0-9 ]", "", l[0]), l[1]) for actor in lst]
F-string - f'{l[0]}, {l[1]}' for l in lst]

desired_output = [(A, 1), (B, 14), (C, 12), (D, 11), (E, 9)] 

>Solution :

Code:

lts = [('A', 1), ('B', 14), ('C', 12), ('D', 11), ('E', 9)]
a = "["
b = []
for l in lts:
    b.append( f"({l[0]},{l[1]})" )
e = ", ".join(b)
print(e)
for each in e:
    a += each
a += f"]"
print(a)

Output

(A,1), (B,14), (C,12), (D,11), (E,9)
[(A,1), (B,14), (C,12), (D,11), (E,9)]

Leave a ReplyCancel reply