1

I have list of values:

md5 = ['1111', '3333', '44444', '555555', '56632423', '23423514', '2342352323']

I want to concatenate them into a Query String:

'md5=1111&md5=3333&md5=44444&md5=555555&md5=56632423&md5=23423514&md5=2342352323'

What is the best way to do that?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
SooIn Nam
  • 3,121
  • 4
  • 21
  • 18
  • 1
    You might first want to try looking at some of the related questions, such as [this one](http://stackoverflow.com/questions/4481724/convert-a-list-of-characters-into-a-string). :) – summea May 10 '13 at 23:04
  • 3
    In the future, you should give higher-level information. Are you building a query string to use in a URL? If so, tell us that, _then_ give us the example, instead of starting at the lowest level. See [What is the XY problem?](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) for why you'll generally get better answers that way (although this time, you got lucky and Zero Piraeus guessed for you). – abarnert May 10 '13 at 23:13

3 Answers3

14

Since you're building a query string, it's better to use a function from the standard library that's specifically designed to do so:

... than to muck around with str.join(). Here's how you'd use it:

from urllib.parse import urlencode # Python 3
# from urllib import urlencode # Python 2

md5 = ['1111', '3333', '44444', '555555', '56632423', '23423514', '2342352323']
urlencode([("md5", x) for x in md5])

Result:

'md5=1111&md5=3333&md5=44444&md5=555555&md5=56632423&md5=23423514&md5=2342352323'

Alternatively (thanks to Jon Clements in the Python chatroom), you can pass a dictionary to urlencode and the parameter doseq=True:

urlencode({'md5': md5}, doseq=True)

... which produces the same result. This is explained in the documentation linked above:

The value element in itself can be a sequence and in that case, if the optional parameter doseq is evaluates to True, individual key=value pairs separated by '&' are generated for each element of the value sequence for the key. The order of parameters in the encoded string will match the order of parameter tuples in the sequence.

Community
  • 1
  • 1
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • 1
    Yeah, if he's building a query string (very likely), this is definitely the best way to do it. You might want to add the fact that it's `urllib.parse.urlencode` instead of `urllib.urlencode` in Python 3.x. – abarnert May 10 '13 at 23:12
5
md5s = '&'.join('md5='+m for m in md5)

The part inside the parentheses is a generator expression, which gives you 'md5='+m for each m in the original list.

The join function takes that expression and links it into one big string, putting an & between each pair.

abarnert
  • 354,177
  • 51
  • 601
  • 671
0

You can use join():

md5=['1111','3333','44444','555555','56632423','23423514','2342352323']
print 'md5=' + '&md5='.join(md5)
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • That doesn't give him the output he asked for. – abarnert May 10 '13 at 23:05
  • 1
    OK, now it works, but… it's a little odd. If you use `join` for the part that joins up elements, everything makes sense. If you try to use it for additional string processing, you end up with fencepost problems where you need to add an extra `md5=` at the beginning (your answer) or remove a `&` at the end (ljlozano's answer), which makes it a lot easier to get something wrong. – abarnert May 10 '13 at 23:08