Python program to convert URL Parameters to Dictionary items Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share 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 moduleThe 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.Table of ContentUsing a dictionary comprehensionUsing the cgi moduleUsing string manipulationUsing a dictionary comprehensionIf 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 modulecgi 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 manipulationIf 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. Comment More infoAdvertise with us Next Article Python | Key-Value to URL Parameter Conversion M manjeet_04 Follow Improve Article Tags : Python Python Programs Python dictionary-programs Python string-programs Practice Tags : python Similar Reads Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2 3 min read Python | Key-Value to URL Parameter Conversion 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 5 min read Convert String Dictionary to Dictionary in Python The goal here is to convert a string that represents a dictionary into an actual Python dictionary object. For example, you might have a string like "{'a': 1, 'b': 2}" and want to convert it into the Python dictionary {'a': 1, 'b': 2}. Let's understand the different methods to do this efficiently.Us 2 min read Convert String to Nested Dictionaries In Python, sometimes a string represents nested dictionary data. For example, the string might look like "{'a': {'b': {'c': 1}}}". The task is to convert this string into an actual nested dictionary. Let's explore several methods to achieve this.Using ast.literal_evalThis method uses the literal_eva 2 min read Convert byte string Key:Value Pair of Dictionary to String - Python In Python, dictionary keys and values are often stored as byte strings when working with binary data or certain encoding formats, we may need to convert the byte strings into regular strings. For example, given a dictionary {b'key1': b'value1', b'key2': b'value2'}, we might want to convert it to {'k 3 min read Iterate Through Dictionary Keys And Values In Python In Python, a Dictionary is a data structure where the data will be in the form of key and value pairs. So, to work with dictionaries we need to know how we can iterate through the keys and values. In this article, we will explore different approaches to iterate through keys and values in a Dictionar 2 min read Like