7

I have a list containing tuples that is generated from a database query and it looks something like this.

[(item1, value1), (item2, value2), (item3, value3),...]

The tuple will be mixed length and when I print the output it will look like this.

item1=value1, item2=value2, item3=value3,...

I have looked for a while to try to find a solution and none of the .join() solutions I have found work for this type of situation.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
lawless
  • 191
  • 1
  • 5
  • 15

6 Answers6

13

You're after something like:

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>> ', '.join('{}={}'.format(*el) for el in a)
'val=1, val2=2, val3=3'

This also doesn't care what type the tuple elements are... you'll get the str representation of them automatically.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
3

If each tuple is only an (item, value) pair then this should work:

l = [(item1, value1), (item2, value2), (item3, value3), ...]
', '.join('='.join(t) for t in l)
'item1=value1, item2=value2, item3=value3, ...'
FastTurtle
  • 2,301
  • 19
  • 19
  • 2
    The inner `join` works on each `(item, value)` pair and turns each one into `item=value`. At that point you essentially have a list of `["item1=value1", "item2=value2", "item3=value3", ...]. The outter `join` works on each of the `"item=value"` and combines them with a comma and a space to form the final result – FastTurtle Aug 20 '13 at 20:12
  • OK because `'='.join('a','b')` == `a=b` so `'='.join(t) for t in l` will give me a list like `["item1=value1", "item2=value2", "item3=value3"..` then you use Outer join to concat, by `,` .Got it Thanks for your response. But I have a doubt. Do we need `*` as `'='.join(t) for t in l` should be `'='.join(*t) for t in l` ?? – Grijesh Chauhan Aug 20 '13 at 20:16
  • 1
    @GrijeshChauhan Using `*` in the `join` will give you an error saying that `join()` takes exactly one argument. The only argument join can take is an iterable `t` in this case. Perhaps @Jon Clements's use of `*` in `format` is confusing you. In that case, without the `*` only one argument (a tuple) would be passed to `format`, which would be used to populate the first `{}`. This would raise an error because the string is expecting **two** positional arguments. Take a look at [this](http://stackoverflow.com/a/2921893/1507867) answer for more information on unpacking arguments in python. – FastTurtle Aug 20 '13 at 20:26
  • I think got it now..So I wrote in comment `'='.join('a','b')` I should be `'='.join( ('a','b') )` **?** Thanks again man! you answer and comment are very helpful to me. – Grijesh Chauhan Aug 20 '13 at 20:31
  • 1
    @GrijeshChauhan Yup! Looks like you got it. One thing to keep in mind is that `join()` takes an iterable, so `'='.join(['a', 'b'])` will also work. For me, trying out these syntax questions in the python interpreter helps a lot. – FastTurtle Aug 20 '13 at 20:49
  • This is a beautiful trick! It also works for `N>2` dimensional tuples! – not2qubit Nov 20 '18 at 11:56
3

You can use itertools as well

from itertools import starmap
', '.join(starmap('{}={}'.format, a))
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
1

Try this:

lst = [('item1', 'value1'), ('item2', 'value2'), ('item3', 'value3')]
print ', '.join(str(x) + '=' + str(y) for x, y in lst)

I'm explicitly converting to string the items and values, if one (or both) are already strings you can remove the corresponding str() conversion:

print ', '.join(x + '=' + y for x, y in lst)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
1

One possible solution is this, definitely the shortest code

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>>', '.join('%s=%s' % v for v in a)
'val=1, val2=2, val3=3'

works with python 2.7 as well

Ati
  • 181
  • 2
  • 9
0

If you want something like that, I would use a dictionary.

dict = {1:2,3:4}
print dict

Then, you can loop through it like this:

dict = {1:2,3:3}
print dict

for i in dict:
    print i, "=", dict[i]

Hope it helps!