Constructing URL Query Parameters in Python
Web applications frequently use query parameters to pass information to a server, such as search terms, filters, or pagination details. In Python, effectively constructing these parameters is a common task when interacting with APIs or building web services. This challenge will test your ability to create well-formed query parameter strings from Python data structures.
Problem Description
Your task is to write a Python function that takes a dictionary of key-value pairs and converts it into a properly formatted URL query string. This string should be appended to a base URL.
Key requirements:
- The function should accept a dictionary where keys are strings and values can be strings, numbers, booleans, or lists of strings/numbers.
- Each key-value pair in the dictionary should be encoded as a
key=valuepair. - Pairs should be joined by an ampersand (
&). - Special characters within keys and values (e.g., spaces,
&,=,?) must be URL-encoded. - If a value is a list, each item in the list should be represented as a separate
key=valuepair, all with the same key. - The function should handle empty dictionaries gracefully.
Expected behavior:
The function will return a string that is ready to be appended to a base URL. For example, if the dictionary is {'q': 'python programming', 'page': 2}, the output should be q=python+programming&page=2.
Edge cases to consider:
- Values that are
Noneshould be ignored. - Empty lists as values should also be ignored.
Examples
Example 1:
Input:
base_url = "https://api.example.com/search"
params = {'q': 'python programming', 'page': 2}
Output: "https://api.example.com/search?q=python+programming&page=2"
Explanation: The dictionary is converted into a query string. Spaces are replaced by '+'.
Example 2:
Input:
base_url = "https://api.example.com/products"
params = {'category': 'electronics', 'tags': ['gadget', 'tech']}
Output: "https://api.example.com/products?category=electronics&tags=gadget&tags=tech"
Explanation: The 'tags' list is expanded into multiple key-value pairs with the same key.
Example 3:
Input:
base_url = "https://api.example.com/items"
params = {'search': 'user & name', 'filter': None, 'empty_list': []}
Output: "https://api.example.com/items?search=user+%26+name"
Explanation: `None` and empty lists are excluded. The '&' character in 'user & name' is URL-encoded as '%26'.
Example 4:
Input:
base_url = "https://api.example.com/data"
params = {}
Output: "https://api.example.com/data"
Explanation: An empty parameters dictionary results in no query string being added.
Constraints
- The input dictionary keys will always be strings.
- The input dictionary values will be strings, integers, booleans, lists of strings/integers,
None, or empty lists. - The base URL will always be a valid URL string.
- The function should be efficient for dictionaries with up to 100 key-value pairs.
Notes
You'll likely find the urllib.parse module in Python's standard library to be very helpful for this task, particularly functions related to URL encoding and parsing. Consider how you will handle different data types for values and how to correctly append the query string to the base URL. Remember that the first query parameter is preceded by a ?, and subsequent parameters by an &.