2

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?

gr0gu3
  • 555
  • 1
  • 6
  • 11

1 Answers1

8

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']}
James
  • 32,991
  • 4
  • 47
  • 70