-1

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

1
  • any attempts? You know ast.literal_eval? Commented Dec 1, 2015 at 5:23

3 Answers 3

2

When A is a list of str:

print(', '.join(A))

Or more general:

print(', '.join(map(str, A)))
Sign up to request clarification or add additional context in comments.

Comments

0

In your case below code will work

print(', '.join(A))

Comments

0

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)'

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.