Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converte ImmutableMultiDict with duplicate keys to list of lists or dictionary [duplicate]

So, basically I'm trying to convert an ImmutableMultiDict object to a list of lists or dictionary, but I'm having a hard time trying to figure out how. The thing is I have two values for the same key, so when I try the conversion, I can only get 1 value:

ImmutableMultiDict([('name', 'boom'), ('extension', 'pdf'), ('extension', 'doc')])

When I try dict(object)

{'name': 'boom', 'extension': 'pdf'}

Any suggestion?

like image 377
gr0gu3 Avatar asked Dec 28 '25 22:12

gr0gu3


1 Answers

The ImmutableMultiDict object has a lists method. You can use that to convert it to a dictionary with unified keys that have lists for the values.

from werkzeug import ImmutableMultiDict

d = ImmutableMultiDict([('name', 'boom'), ('extension', 'pdf'), ('extension', 'doc')])
dict(d.lists())
# returns:
{'name': ['boom'], 'extension': ['pdf', 'doc']}
like image 197
James Avatar answered Dec 31 '25 11:12

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!