Open In App

Python program to convert URL Parameters to Dictionary items

Last Updated : 10 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

URL parameters are often used to pass data between web pages and converting them into a dictionary makes it easier to work with the data programmatically. In this article, we will learn how to convert URL parameters into dictionary items in Python.

Using urllib.parse module

The urllib.parse module provides built-in tools to work with URLs and query strings. We can use the parse_qs() function to convert URL parameters into a dictionary.

Python
from urllib.parse import parse_qs

url = "https://example.com/page?name=John&age=25&city=NewYork"

res = parse_qs(url.split("?")[1])
print(res)

Output
{'name': ['John'], 'age': ['25'], 'city': ['NewYork']}

Explanation:

  • We split the URL at the question mark to separate the query string.
  • The parse_qs function parses the query string and converts it into a dictionary, where values are stored as lists.

Let’s explore some more methods and see how we can convert url parameters to dictionary items.

Using a dictionary comprehension

If we want more control or do not want to use an external module, we can manually process the query string and convert it into a dictionary.

Python
url = "https://example.com/page?name=John&age=25&city=NewYork"

query = url.split("?")[1]

res = {x.split("=")[0]: x.split("=")[1] for x in query.split("&")}
print(res)

Output
{'name': 'John', 'age': '25', 'city': 'NewYork'}

Explanation:

  • We split the URL at the question mark to get the query string.
  • The query string is further split into key-value pairs using the ampersand as a separator.
  • Each key-value pair is split using the equals sign, and a dictionary is built using a dictionary comprehension.

Using the cgi module

cgi module also provides tools for handling query strings. We can use the parse function to convert URL parameters into a dictionary.

Python
import cgi

url = "https://example.com/page?name=John&age=25&city=NewYork"
query = url.split("?")[1]
res = cgi.parse_qs(query)
print(res)

Explanation:

  • We split the URL at the question mark to extract the query string.
  • The cgi.parse_qs function parses the query string and converts it into a dictionary, where values are stored as lists.

Using string manipulation

If the URL parameters are simple, we can use basic string operations to manually convert them into a dictionary.

Python
url = "https://example.com/page?name=John&age=25&city=NewYork"

query = url.split("?")[1]

res = {}

for pair in query.split("&"):
    key, value = pair.split("=")
    res[key] = value
print(res)

Output
{'name': 'John', 'age': '25', 'city': 'NewYork'}

Explanation:

  • We split the URL at the question mark to separate the query string.
  • The query string is split into key-value pairs using the ampersand as a separator.
  • Each pair is further split using the equals sign, and the key-value pairs are added to the dictionary.


Next Article

Similar Reads