[Answered ]-Python cant make dictionary from list of dictionaries

2👍

You were pretty close. First I would use enumerate because that what it meant to do. And use iteritems(python 2.7) to iterate over the dict.:

for idx, _dict in enumerate(err):
    for error_key, error_value in _dict.iteritems():
        results[str(idx)+'-' + error_key] = error_value

print results

and I got:

{‘1-field’: [‘This field is required.’], ‘2-field’: [‘This field is required.’]}

*As mentioned on the comments – Don’t use dict since it’s preserved word on python.

0👍

err = formset.errors
D = {}
for i in len(err):
    crr_field = err[i].keys()[0]
    error_msg  = "{field} error: {error}".format(field=crr_field,error=err[i][crr_field])
    D[i] = error_msg

D will be {1:"Field field1 error: field is required",2:....}

0👍

Try this

results = {}
err = formset.errors
for i, my_dict in enumerate(err):
    for key, value in my_dict.items():
        results[str(i)+'-'+key] = value

items() works in python3, because iteritems() was removed.

👤doru

Leave a comment