28

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?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
ssoler
  • 4,884
  • 4
  • 32
  • 33
  • 3
    What are these tuples for, why do they need to be in a string, and do they in fact need to be tuples? – Ignacio Vazquez-Abrams Jul 20 '10 at 17:31
  • 1
    why would you want to do this? what is your real problem? – SilentGhost Jul 20 '10 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. – polygenelubricants Jul 20 '10 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. – S.Lott Jul 20 '10 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. – ssoler Jul 21 '10 at 07: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. – Ignacio Vazquez-Abrams Jul 21 '10 at 08:11
  • @Ignacio Vazquez-Abrams: Thanks for the advice. I'm aware of this. It's just a quick and dirty script to move some data. – ssoler Jul 21 '10 at 13:07
  • Does anyone have a link for the question that does the exact opposite (i.e., string to tuple)? – cs95 Jul 06 '20 at 08:28

7 Answers7

38

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)
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
37

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

mykhal
  • 19,175
  • 11
  • 72
  • 80
21

How about:

>>> tups = [(1, 2), (3, 4)]
>>> ', '.join(map(str, tups))
'(1, 2), (3, 4)'
pillmuncher
  • 10,094
  • 2
  • 35
  • 33
2

The most pythonic solution is

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

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

result = ', '.join(tuple_strings)
ValarDohaeris
  • 6,064
  • 5
  • 31
  • 43
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.

Maehler
  • 6,111
  • 1
  • 41
  • 46
1

How about

l = [(1, 2), (3, 4)]
print repr(l)[1:-1]
# (1, 2), (3, 4)
Benj
  • 1,853
  • 4
  • 18
  • 27
  • 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. – Benj Jul 20 '10 at 17:47
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)'
blaztinn
  • 343
  • 4
  • 8