Skip to content Skip to sidebar Skip to footer

Convert Querydict To Key-value Pair Dictionary

I have a QueryDict that I get from request.POST in this format:

Solution 1:

You can use list comprehension to get the first entry from the value lists;

dd = dict(request.POST)
ddnew = {k:dd[k][0] for k in dd}

Solution 2:

You may try this

my_dict = dict(request.POST)
my_dict = {k: v[0] for k, v in my_dict.items()}
print(my_dict)

Assume for all dictionary values are lists that only has one element.

Post a Comment for "Convert Querydict To Key-value Pair Dictionary"