How to Parse Query String to Dict with Duplicate Keys in Python
Convert a URL query string into a Python dictionary, merging duplicate keys into lists while keeping single values as scalars.
Python code
12 linesfrom urllib.parse import parse_qs
def parse_query_to_dict(query_string):
parsed = parse_qs(query_string, keep_blank_values=True)
return {key: values if len(values) > 1 else values[0] for key, values in parsed.items()}
if __name__ == "__main__":
query = "name=John&name=Jane&age=30&city=&city=Paris&empty_prop="
result = parse_query_to_dict(query)
print(result)
Output
{'name': ['John', 'Jane'], 'age': '30', 'city': ['', 'Paris'], 'empty_prop': ''}
How it works
The parse_qs function from urllib.parse splits a query string into key-value pairs, automatically handling URL decoding and duplicate keys by grouping values into lists. The dictionary comprehension then converts single-element lists back to plain values while keeping multi-element lists intact. keep_blank_values=True ensures empty values like city= and empty_prop= are preserved in the output. This approach keeps the result compact for common single-value parameters while explicitly showing duplicates.
Common mistakes
- Forgetting `keep_blank_values=True` drops empty parameters like `city=`
- Assuming duplicate keys are lost; always remember parse_qs groups them into lists
- Overlooking that all values are lists by default, requiring conversion logic
- Not URL-decoding values, though parse_qs handles percent-encoding automatically
Variations
- Use `urllib.parse.parse_qsl` and manually group with a defaultdict for custom merge rules
- Return flat lists for all keys: `{k: v for k, v in parse_qs(q).items()}` if uniform handling is preferred
Real-world use cases
- Parsing multi-value form submissions like checkboxes or multi-select filters in a web framework.
- Extracting repeated query parameters in API request handling where users may pass the same filter twice.
- Normalizing incoming webhook URLs for analytic tracking where campaign tags can appear more than once.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.