How to sort a dictionary of dictionaries where the key is an object?

have a dictionary like this:
dictionary
{
account.account(354,
 ): {
 'period_credit': 0.0,
 'code': u'27101',
 'period_debit': 100.0,
 'name': u'\u0160B LT577180900027467092',
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 100.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(225485,
 ): {
 'period_credit': 0.0,
 'code': u'27101',
 'period_debit': 100.0,
 'name': u jonavos tarpine kasa',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 100.0,
 'start_debit': 0.0,
 'end_balance': 100.0
}
},
 'end_balance': 100.0
},
account.account(420,
 ): {
 'period_credit': 100.0,
 'code': u'273012',
 'period_debit': 100.0,
 'name': u Jonavos tarpin\u0117 kasa',
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(48341,
 ): {
 'period_credit': 0.0,
 'code': u'273012',
 'period_debit': 100.0,
 'name': u'i\u012e AGAVA IR KO',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 100.0,
 'start_debit': 0.0,
 'end_balance': 100.0
},
Res. partner(225485,
 ): {
 'period_credit': 100.0,
 'code': u'273012',
 'period_debit': 0.0,
 'name': u jonavos tarpine kasa',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 100.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': -100.0
}
},
 'end_balance': 0.0
},
account.account(124,
 ): {
 'period_credit': 0.0,
 'code': u'2010',
 'period_debit': 0.4,
 'name': u'\u017daliav\u0173, med\u017eiag\u0173 ir komplektavimo items\u0173 \u012fsigijimo savikaina',
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.4,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(205090,
 ): {
 'period_credit': 0.0,
 'code': u'2010',
 'period_debit': 0.4,
 'name': u PIRK\u0116JAI',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.4,
 'start_debit': 0.0,
 'end_balance': 0.4
}
},
 'end_balance': 0.4
},
account.account(300,
 ): {
 'period_credit': 0.4,
 'code': u'600000',
 'period_debit': 0.0,
 'name': u Parduot\u0173 preki\u0173 savikaina',
 'start_balance': 0.0,
 'end_credit': 0.4,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(205090,
 ): {
 'period_credit': 0.4,
 'code': u'600000',
 'period_debit': 0.0,
 'name': u PIRK\u0116JAI',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.4,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': -0.4
}
},
 'end_balance': -0.4
},
account.account(281,
 ): {
 'period_credit': 1.18,
 'code': u'500000',
 'period_debit': 1.18,
 'name': u Parduot\u0173 preki\u0173 pajamos',
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(205090,
 ): {
 'period_credit': 1.18,
 'code': u'500000',
 'period_debit': 1.18,
 'name': u PIRK\u0116JAI',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': 0.0
}
},
 'end_balance': 0.0
},
account.account(156,
 ): {
 'period_credit': 101.43,
 'code': u'241000',
 'period_debit': 1.43,
 'name': u Pirk\u0117j\u0173 skol\u0173 vert\u0117',
 'start_balance': 0.0,
 'end_credit': 100.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(205090,
 ): {
 'period_credit': 1.43,
 'code': u'241000',
 'period_debit': 1.43,
 'name': u PIRK\u0116JAI',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': 0.0
},
Res. partner(48341,
 ): {
 'period_credit': 100.0,
 'code': u'241000',
 'period_debit': 0.0,
 'name': u'i\u012e AGAVA IR KO',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 100.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': -100.0
}
},
 'end_balance': -100.0
},
account.account(275,
 ): {
 'period_credit': 0.25,
 'code': u'449200',
 'period_debit': 0.25,
 'name': u Mok\u0117tinas prid\u0117tin\u0117s vert\u0117s mokestis',
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'partner_dicts': {
Res. partner(205090,
 ): {
 'period_credit': 0.25,
 'code': u'449200',
 'period_debit': 0.25,
 'name': u PIRK\u0116JAI',
 'partner_code': False,
 'start_balance': 0.0,
 'end_credit': 0.0,
 'start_credit': 0.0,
 'end_debit': 0.0,
 'start_debit': 0.0,
 'end_balance': 0.0
}
},
 'end_balance': 0.0
}
}

how do I filter it on the name field? tried here so:
sorted_account_dicts = sorted(account_dicts, key=lambda x: account_dicts[x].get('name', float('inf')))
sorted_account_dicts = sorted(account_dicts, key=itemgetter('name'))
sorted_account_dicts = sorted(account_dicts.items(), key=lambda x: x[1]['name'], reverse=False)

everywhere writes:
QWebException: "list indices must be integers, not tuple" while evaluating "data_vals['account_dicts'][account]"
here is a code fragment with xml:
<t t-foreach="data_vals['account_dicts']" t-as="account">
 <t t-set="account_dict" t-value="data_vals['account_dicts'][account]"/>
 <tr style="height: 15px; font-size: 6pt; font-weight: bold; text-align: right; vertical-align: top;">
 <td style="font-size: 7pt; text-align: left;">
 <span t-esc="account['code']"/>
</td>
 <td style="font-size: 7pt; text-align: left;">
 <span t-esc="account['name']"/>
</td>
 ... other code...
March 12th 20 at 07:56
1 answer
March 12th 20 at 07:58
Solution
the problem was solved like this:
from collections import OrderedDict

....

sorted_account_dicts = OrderedDict(sorted(account_dicts.items(), key=lambda x: x[1]['name'], reverse=False))

Find more questions by tags Python