I have a python dictionary that I want to compress into a diffrent dict based on the keys:
{'field1_0': 'FieldName1', 'field2_0': 'DataType1',
'field1_1': 'FieldName 2', 'field2_1': 'DataType2'}
In this post my keys are automated form field names and they are grouped by the digit provided after "_" : field1_0
, field2_0
are grouped and field1_1
, field2_1
are grouped because of the trailing number.
I want to take that and combine those grouped elements and compress them into a dict of key value pairs where each value for the grouped item is in the new dict.
For example, take the above dict. I want a dict that looks like this:
{'FieldName1': 'DataType1', 'FieldName2': 'DataType2'}
For context with what I am doing: I have a few dynamic Django forms that allow users to create custom reports in my project. To do this I have 1 form that allows them to enter a number for the number of fields they want. When this form is submitted, a dynamic form creates 2 fields per number of elements entered (1 for the field name and 1 for the data type, i.e. 'field1_{x}': 'field name'
and 'field2_{x}':'datatype'
). I will take this submission which creates the dict we are discussing and pass it into a different dynamic form which creates the fields based on field name and data type, then viola you've got custom forms that can be created based on 3rd party declarations.