30

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?

7
  • 3
    What are these tuples for, why do they need to be in a string, and do they in fact need to be tuples? Commented Jul 20, 2010 at 17:31
  • 1
    @Ignacio, @SilentGhost: I'd love to have you guys elaborate more on your comments (I'm still learning Python myself). It may not be an actual answer to OP's string formatting problem, but I'm sure you guys have very important points to make. Commented Jul 20, 2010 at 19:34
  • 1
    @polygenelubricants: Bottom line: there's no point to this. The tuple -- as a tuple -- is a fine structure. Why mess with it to make an obscurely formatted string? If all they want is a string, then the string.format method will do the job pretty simply. If they want something else, then the question should say what they're tying to accomplish. Commented Jul 20, 2010 at 20:22
  • @Ignacio Vazquez-Abrams, @SilentGhost: This string is the last part of a SQL query string: 'INSERT INTO table (field1, field) VALUES %s' % s. I know there are good frameworks out there like SQLAlchmey or Django, to do this kind of things, but I don't want to use it in this case. Commented Jul 21, 2010 at 7:59
  • 1
    Wait, what? You want to introduce SQL injection attacks into your code? Python gives you the tools to do it right and you want to go out of your way to do it wrong? I have no words. Commented Jul 21, 2010 at 8:11

7 Answers 7

41

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

Sign up to request clarification or add additional context in comments.

5 Comments

What if the tuples are tuples of strings which contain []?
@Rob: str.strip() only removes from the ends.
It seems like bad practice to rely on the internal string representation of a list.
@mykhal it cause some problem with unicode caracters
Not going to work either if you want to join with a different separator.
39

You can try something like this (see also on ideone.com):

myList = [(1,2),(3,4)]
print ",".join("(%s,%s)" % tup for tup in myList)
# (1,2),(3,4)

Comments

21

How about:

>>> tups = [(1, 2), (3, 4)]
>>> ', '.join(map(str, tups))
'(1, 2), (3, 4)'

Comments

2

I think the most Pythonic solution is

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

tuple_strings = ['(%s, %s)' % tuple for tuple in tuples]

result = ', '.join(tuple_strings)

Comments

1

How about

l = [(1, 2), (3, 4)]
print repr(l)[1:-1]
# (1, 2), (3, 4)

1 Comment

The spacing here isn't actually as you stipulated; if that's important, this approach won't work as python adds a space after each item in a list/tuple.
1

I think this is pretty neat:

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

Try it, it worked like a charm for me.

Comments

0

Three more :)

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

unicode(l)[1:-1]
# u'(1, 2), (3, 4)'

("%s, "*len(l) % tuple(l))[:-2]
# '(1, 2), (3, 4)'

", ".join(["%s"]*len(l)) % tuple(l)
# '(1, 2), (3, 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.