-2

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.

ViaTech
  • 2,143
  • 1
  • 16
  • 51
  • I think the idea is that `fieldx_0` is a key with value `fieldx_1` for `x = 1, 2, ...`. – chepner Jan 07 '19 at 21:51
  • got it thanks, still think you have a mis-placed `' '` though – Chris_Rands Jan 07 '19 at 21:53
  • So you are grouping on the second number; following the `_`. Split your keys, use the last part as a key in a new dictionary to track the groups, make the values a list of two elements for key and value, assign to that list based on the first number in your keys (`1` -> index 0, `2` -> index 1, so use `int()` and subtract 1). – Martijn Pieters Jan 07 '19 at 21:56
  • Or, use a decent form abstraction library like WTForms and have it handle mapping between request data and a Python data model for you. – Martijn Pieters Jan 07 '19 at 21:56

1 Answers1

6

Under the assumption that the fields are continuously numbered and always properly paired, you can use a simple dict comprehension:

d = {'field1_0': 'FieldName1', 'field2_0': 'DataType1', 
     'field1_1': 'FieldName 2', 'field2_1': 'DataType2'}

new_d = {d[f'field1_{x}']: d[f'field2_{x}'] for x in range(len(d)//2)}

result:

{'FieldName1': 'DataType1', 'FieldName2': 'DataType2'}
martineau
  • 119,623
  • 25
  • 170
  • 301
chepner
  • 497,756
  • 71
  • 530
  • 681
  • is it just me or does this code have a syntax error – Feelsbadman Jan 07 '19 at 21:58
  • 1
    @Feelsbadman: I see no obvious syntax errors. You'll have to be more explicit (I fixed a minor division issue, the code runs fine in Python 3.6 and newer). – Martijn Pieters Jan 07 '19 at 21:58
  • I used Python 3.7's `f`-strings to simplify the code. Adjust the string formatting as appropriate for your version of Python. – chepner Jan 07 '19 at 21:59
  • @chepner: Python 3.6 introduced `f`-strings. – Martijn Pieters Jan 07 '19 at 22:00
  • This is exactly what I needed, thank you guys. Not understanding the downvote on my end, but nevertheless this is the correct answer for my issue so I will leave this as a posted question with this as the answer. – ViaTech Jan 07 '19 at 22:02