Many times, while working in web development domain, we can encounter a problem in which we require to set as URL parameter some of the key-value pairs we have, either in form of tuples, or a key and value list. Let’s discuss a solution for both the cases.
Method #1 : Using urllib.urlencode() ( with tuples )
The urlencode function is root function that can perform the task that we wish to achieve. In case of tuples, we can just pass the tuples and encoder does the rest of conversion of string. Works only with Python2.
# Python code to demonstrate working of # Key-Value to URL Parameter Conversion # Using urllib.urlencode() ( with tuples ) import urllib # initializing tuples test_tuples = (('Gfg', 1), ('is', 2), ('best', 3)) # printing original tuples print("The original tuples are : " + str(test_tuples)) # Using urllib.urlencode() ( with tuples ) # Key-Value to URL Parameter Conversion res = urllib.urlencode(test_tuples) # printing URL string print("The URL parameter string is : " + str(res)) |
The original tuples are : (('Gfg', 1), ('is', 2), ('best', 3))
The URL parameter string is : Gfg=1&is=2&best=3
Method #2 : Using urllib.urlencode() ( with dictionary value list )
This method is when we have a dictionary key and many values corresponding to them as a potential candidate for being the URL parameter. In this case we perform this function. This also works with just Python2.
# Python code to demonstrate working of # Key-Value to URL Parameter Conversion # Using urllib.urlencode() ( with dictionary value list ) import urllib # initializing dictionary test_dict = {'gfg' : [1, 2, 3]} # printing original dictionary print("The original dictionary is : " + str(test_dict)) # Using urllib.urlencode() ( with dictionary value list ) # Key-Value to URL Parameter Conversion res = urllib.urlencode(test_dict, doseq = True) # printing URL string print("The URL parameter string is : " + str(res)) |
The original dictionary is : {'gfg': [1, 2, 3]}
The URL parameter string is : gfg=1&gfg=2&gfg=3
Recommended Posts:
- Python | sep parameter in print()
- Python end parameter in print()
- Python | file parameter in print()
- Use of na_values parameter in read_csv() function of Pandas in Python
- Python | Split and Pass list as separate parameter
- Type Conversion in Python
- C strings conversion to Python
- Python | Timezone Conversion
- Python | Tuple key dictionary conversion
- Python: Weight Conversion GUI using Tkinter
- Python | Conversion to N*N tuple matrix
- Python | Empty String to None Conversion
- Python | Type conversion in dictionary values
- HTML Cleaning and Entity Conversion | Python
- Python | Decimal to binary list conversion
- Python | Type conversion of dictionary items
- Python | List of float to string conversion
- Python | Dictionary to list of tuple conversion
- Python | List of tuples to dictionary conversion
- Python - Tuple to Dictionary Summation conversion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

