Search code examples
pythonliststring-formattingtuples

Python - convert list of tuples to string


Which is the most pythonic way to convert a list of tuples to string?

I have:

[(1,2), (3,4)]

and I want:

"(1,2), (3,4)"

My solution to this has been:

l=[(1,2),(3,4)]
s=""
for t in l:
    s += "(%s,%s)," % t
s = s[:-1]

Is there a more pythonic way to do this?


Solution

  • you might want to use something such simple as:

    >>> l = [(1,2), (3,4)]
    >>> str(l).strip('[]')
    '(1, 2), (3, 4)'
    

    .. which is handy, but not guaranteed to work correctly