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?

2
  • 1
    You might first want to try looking at some of the related questions, such as this one. :) Commented May 10, 2013 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? for why you'll generally get better answers that way (although this time, you got lucky and Zero Piraeus guessed for you). Commented May 10, 2013 at 23:13

3 Answers 3

15

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.

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

1 Comment

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.
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.

Comments

0

You can use join():

md5=['1111','3333','44444','555555','56632423','23423514','2342352323']
print 'md5=' + '&md5='.join(md5)

2 Comments

That doesn't give him the output he asked for.
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.

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.