-2

I got a list problem:

 position = self.request.POST.getlist('position')
 status = self.request.POST.getlist('tooth')

 a = dict((i, j) for i, j in zip(position, status) if j != '')

 print(a)
 {'14': 'status1', '15': 'status2', '13': 'status3'}

Is it possible to achieve format result like:

  {'14': [status1], [15]: [status2], [13]: [status3]}

Please.

Carlos Fabiera
  • 79
  • 1
  • 1
  • 10
  • status1, status2, status3 are variables? – Ashish Acharya Oct 14 '17 at 04:15
  • 6
    python dictionaries cannot have lists as keys like in your desired output. See [here](https://stackoverflow.com/questions/7257588/why-cant-i-use-a-list-as-a-dict-key-in-python). – kaza Oct 14 '17 at 04:16
  • if you want the values to be lists, you could do something like: a = dict((i, [j]) for i, j in zip(position, status) if j != '') – Ashish Acharya Oct 14 '17 at 04:17
  • You can certainly _print_ the data like that, by using the appropriate formatting commands, but are you sure you really want that exact format? – PM 2Ring Oct 14 '17 at 04:25

1 Answers1

2

This should do it:

{k:[v] for k, v in a.items()}

This results in:

{'13': ['status3'], '15': ['status2'], '14': ['status1']}

Note that the keys are unchanged. Your example shows some of them being converted to integers and embedded in lists, but lists cannot be dictionary keys.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41