2

I have a list of dictionaries:

AccountValues = [
{'portfolio_ref': 1, 'tag': 'FullInit', 'value': '20642.95', 'currency': 'USD', 'percent': 0.0}, 
{'portfolio_ref': 1, 'tag': 'FullMaint', 'value': '21350.54', 'currency': 'USD', 'percent': 0.0}, 
{'portfolio_ref': 1, 'tag': 'NetLiq', 'value': '70976.05', 'currency': 'USD', 'percent': 100.0} ]

Simple mission per SQL description: Order by portfolio_ref ASC, percent DESC

What I tried unsuccessfully:

sorted(AccountsValues, key=lambda x: (x[1],-x[4]))

which gives me

KeyError: 1

Second attempt:

import operator
result = sorted(myAccountsValues, key=itemgetter('percent'))

which fails to sort on percentage.

2
  • What about what you tried didn't work? It's always helpful to be clear about what you tried, what you expected, and what you got instead. For instance did you get an error message? Or just the wrong result? Did you make any attempts at debugging? Commented Sep 25, 2018 at 23:01
  • The reason your second attempt didn't seem to work is that your data is already sorted on percentage... Commented Sep 25, 2018 at 23:12

2 Answers 2

3

You can use dict.__getitem__ or its syntactic sugar []:

res = sorted(AccountValues, key=lambda x: (x['portfolio_ref'], -x['percent']))

Remember that dictionaries are not indexable by integers. Historically (pre-3.6), they are not even ordered. Even in Python 3.7, you cannot directly extract the nth key or value.

Result:

print(res)

[{'portfolio_ref': 1, 'tag': 'NetLiq', 'value': '70976.05', 'currency': 'USD', 'percent': 100.0},
 {'portfolio_ref': 1, 'tag': 'FullInit', 'value': '20642.95', 'currency': 'USD', 'percent': 0.0},
 {'portfolio_ref': 1, 'tag': 'FullMaint', 'value': '21350.54', 'currency': 'USD', 'percent': 0.0}]
Sign up to request clarification or add additional context in comments.

Comments

1

You just have to combine all the things you did correctly: sort keys as a tuple and the proper way of referencing a dict entry:

>>> sorted(AccountValues, key=lambda x: (x["portfolio_ref"], -x["percent"]))
[{'tag': 'NetLiq', 'portfolio_ref': 1, 'value': '70976.05', 'percent': 100.0, 'currency': 'USD'},
 {'tag': 'FullInit', 'portfolio_ref': 1, 'value': '20642.95', 'percent': 0.0, 'currency': 'USD'},
 {'tag': 'FullMaint', 'portfolio_ref': 1, 'value': '21350.54', 'percent': 0.0, 'currency': 'USD'}]

Better yet, use

sorted(AccountValues, key=itemgetter("portfolio_ref", "percent"))

Your first attempt failed because x[1] and x[4] are not valid references into the dictionaries: you have to use the labels you originally gave, not relative positions.

Your second attempt is deficient only because you don't have the secondary sort key.

1 Comment

As I was pasting, also note @jpp posted same response, gj to both

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.