Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Let's say that I have this list in python
A = ["(a,1)", "(b,2)", "(c,3)", "(d,4)"]
so how can I print it out in the following format:
(a,1), (b,2), (c,3), (d,4)
using one line, better without using for loop Thanks in advance
ast.literal_eval
When A is a list of str:
print(', '.join(A))
Or more general:
print(', '.join(map(str, A)))
Add a comment
In your case below code will work
You can also use regular expression:
>>> A = ["(a,1)", "(b,2)", "(c,3)", "(d,4)"] >>> import re >>> re.sub(r'[\[\]\"]', r"",",".join(A)) '(a,1),(b,2),(c,3),(d,4)'
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
ast.literal_eval?