Python - convert list of tuples to string
Asked Answered
Y

7

29

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?

Yellows answered 20/7, 2010 at 17:28 Comment(7)
What are these tuples for, why do they need to be in a string, and do they in fact need to be tuples?Descendent
@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.Sharpeared
@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.Crackbrained
@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.Yellows
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.Descendent
@Ignacio Vazquez-Abrams: Thanks for the advice. I'm aware of this. It's just a quick and dirty script to move some data.Yellows
Does anyone have a link for the question that does the exact opposite (i.e., string to tuple)?Sicilia
V
40

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

Vaporescence answered 20/7, 2010 at 17:34 Comment(5)
What if the tuples are tuples of strings which contain []?Laciniate
@Rob: str.strip() only removes from the ends.Descendent
It seems like bad practice to rely on the internal string representation of a list.Michelle
@Vaporescence it cause some problem with unicode caractersPetulancy
Not going to work either if you want to join with a different separator.Trismus
S
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)
Sharpeared answered 20/7, 2010 at 17:30 Comment(0)
L
21

How about:

>>> tups = [(1, 2), (3, 4)]
>>> ', '.join(map(str, tups))
'(1, 2), (3, 4)'
Legislator answered 20/7, 2010 at 19:13 Comment(0)
N
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)
Northampton answered 6/6, 2018 at 18:29 Comment(0)
V
1

How about

l = [(1, 2), (3, 4)]
print repr(l)[1:-1]
# (1, 2), (3, 4)
Voltaire answered 20/7, 2010 at 17:44 Comment(1)
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.Voltaire
P
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.

Pardew answered 3/7, 2012 at 7:10 Comment(0)
L
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)'
Luminosity answered 19/8, 2010 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.