Open In App

Python | Key-Value to URL Parameter Conversion

Last Updated : 04 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Many times, while working in the web development domain, we can encounter a problem in which we require to set as URL parameters 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 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 the case of tuples, we can just pass the tuples and encoder does the rest of conversion of string. Works only with Python2. 

Python




# 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))


Output

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




# 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))


Output

The original dictionary is : {'gfg': [1, 2, 3]}
The URL parameter string is : gfg=1&gfg=2&gfg=3

Method #3 : Using list(),map() and join() methods

Python3




# Python code to demonstrate working of
# Key-Value to URL Parameter Conversion
 
# initializing tuples
test_tuples = (('gfg', 1), ('is', 2), ('best', 3))
 
# printing original tuples
print("The original tuples are : " + str(test_tuples))
 
# Key-Value to URL Parameter Conversion
res = []
for i in test_tuples:
    x=list(map(str,i))
    a="=".join(x)
    res.append(a)
res="&".join(res)
 
# printing URL string
print("The URL parameter string is : " + str(res))


Output

The original tuples are : (('gfg', 1), ('is', 2), ('best', 3))
The URL parameter string is : gfg=1&is=2&best=3

Method #4: Using reduce() function with lambda function

Step-by-step algorithm:

  1. Import the necessary modules.
  2. Initialize the tuples.
  3. Apply lambda function with reduce() function to convert tuples into URL parameter string.
    a. Map the key-value pairs of each tuple into a string with ‘=’ in between.
    b. Join the list of mapped strings with ‘&’ separator using lambda function and reduce() function.
  4. Print the URL parameter string.

Python3




#Using reduce() function with lambda function
import urllib
from functools import reduce
 
#initializing tuples
test_tuples = (('Gfg', 1), ('is', 2), ('best', 3))
 
#printing original tuples
print("The original tuples are : " + str(test_tuples))
 
#Using reduce() function with lambda function
#Key-Value to URL Parameter Conversion
res = reduce(lambda x, y: str(x) + '&' + str(y), ['='.join(map(str, tpl)) for tpl in test_tuples])
 
#printing URL string
print("The URL parameter string is : " + str(res))


Output

The original tuples are : (('Gfg', 1), ('is', 2), ('best', 3))
The URL parameter string is : Gfg=1&is=2&best=3

Time Complexity: O(n) where n is the number of tuples.
Space Complexity: O(n) where n is the number of tuples.

Method #5: Using numpy:

Algorithm:

  1. Initialize the input tuple containing key-value pairs.
  2. Initialize an empty list ‘res’ to store the URL parameter string.
  3. Loop through each tuple in the input tuple list:
    a. Convert each key-value pair to a list of strings.
    b. Join the list with “=” delimiter to form the URL parameter.
    c. Append the parameter to the ‘res’ list.
  4. Join the ‘res’ list with “&” delimiter to form the final URL parameter string.
  5. Print the final URL parameter string.

Python3




import numpy as np
 
# initializing tuples
test_tuples = (('gfg', 1), ('is', 2), ('best', 3))
 
# printing original tuples
print("The original tuples are : " + str(test_tuples))
 
# Key-Value to URL Parameter Conversion using numpy
res = "&".join(np.array(list(map(lambda i: "=".join(list(map(str,i))), test_tuples))))
 
# printing URL string
print("The URL parameter string is : " + str(res))
#This code is contributed by Jyothi pinjala


Output:
The original tuples are : (('gfg', 1), ('is', 2), ('best', 3))
The URL parameter string is : gfg=1&is=2&best=3

Time complexity :

The time complexity of the for loop is O(n), where ‘n’ is the length of the input tuple.
The conversion of key-value pairs to list of strings takes constant time, and joining the list with “=” delimiter also takes constant time.
The join operation to combine the list of URL parameters with “&” delimiter also takes O(n) time.
Therefore, the overall time complexity of the algorithm is O(n).

Auxiliary Space :

The space complexity of the algorithm is O(n), as we are storing each URL parameter in a separate list, and then joining all the parameters to form the final string.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads