165

I am trying to convert a Python dictionary to a string for use as URL parameters. I am sure that there is a better, more Pythonic way of doing this. What is it?

x = ""
for key, val in {'a':'A', 'b':'B'}.items():
    x += "%s=%s&" %(key,val)
x = x[:-1]
Marcellinov
  • 311
  • 7
  • 18
kzh
  • 19,810
  • 13
  • 73
  • 97

5 Answers5

315

Use urllib.parse.urlencode(). It takes a dictionary of key-value pairs, and converts it into a form suitable for a URL (e.g., key1=val1&key2=val2).

For your example:

>>> import urllib.parse
>>> params = {'a':'A', 'b':'B'}
>>> urllib.parse.urlencode(params)
'a=A&b=B'

If you want to make a URL with repetitive params such as: p=1&p=2&p=3 you have two options:

>>> a = (('p',1),('p',2), ('p', 3))
>>> urllib.parse.urlencode(a)
'p=1&p=2&p=3'

or:

>>> urllib.parse.urlencode({'p': [1, 2, 3]}, doseq=True)
'p=1&p=2&p=3'

If you are still using Python 2, use urllib.urlencode().

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 4
    if you want to make a url with repetitive params for example: ?p=1&p=2&p=3 then a = (('p',1),('p',2), ('p', 3)); urllib.urlencode(a) the result is 'p=1&p=2&p=3' – panchicore Jun 27 '12 at 17:05
  • 7
    Another way to get repetitive params: urllib.urlencode({'p': [1, 2, 3]}, doseq=True) resulting in 'p=1&p=2&p=3'. – mbaechtold Apr 16 '14 at 10:51
  • If you wonder what `doeseq` is about: "If any values in the query arg are sequences and doseq is true, each sequence element is converted to a separate parameter." – Martin Thoma Sep 14 '17 at 09:43
  • 8
    Python3 users: `urllib.parse.urlencode()` – Olshansky Mar 15 '21 at 00:03
37

For python 3, the urllib library has changed a bit, now you have to do:

from urllib.parse import urlencode


params = {'a':'A', 'b':'B'}

urlencode(params)
Newskooler
  • 3,973
  • 7
  • 46
  • 84
21

Here is the correct way of using it in Python 3.

from urllib.parse import urlencode
params = {'a':'A', 'b':'B'}
print(urlencode(params))
Atharva
  • 6,711
  • 5
  • 31
  • 39
  • Since python 2 is now deprecated I would accept this answer but stack overflow is preventing me from changing the accepted answer. – kzh Dec 11 '21 at 16:42
  • How is this different from my answer which was ~ a year earlier before this one? – Newskooler Aug 13 '22 at 08:06
3

Use the 3rd party Python url manipulation library furl:

f = furl.furl('')
f.args = {'a':'A', 'b':'B'}
print(f.url) # prints ... '?a=A&b=B'

If you want repetitive parameters, you can do the following:

f = furl.furl('')
f.args = [('a', 'A'), ('b', 'B'),('b', 'B2')]
print(f.url) # prints ... '?a=A&b=B&b=B2'
commadelimited
  • 5,656
  • 6
  • 41
  • 77
Mayank Jaiswal
  • 12,338
  • 7
  • 39
  • 41
-9

This seems a bit more Pythonic to me, and doesn't use any other modules:

x = '&'.join(["{}={}".format(k, v) for k, v in {'a':'A', 'b':'B'}.items()])
Tao Starbow
  • 361
  • 3
  • 10
  • 18
    This won't percent encode the parameters properly. This will create unexpected results if your data includes ampersands, equals, hash symbols, etc. – Jamie Cockburn May 22 '17 at 10:26