0

So I have a dictionary called 'useraccounts'. I manage to obtain all of the keys of the items in the dictionary that are in class 12 by doing:

found = ([k for k in useraccounts if useraccounts[k]['class'] == '12'])

The list returned is:

['bob', 'terry'] 

which is correct. Now is there anyway I could take these results as keys and then seperately use them to individually print relevant information from the dictionary. For example, get the program to print the results of:

useraccounts[THE_KEYS_FOUND_IN_THE_LIST]['totalscore']

Any help is greatly appreciated.

Thanks in advance.

asked Feb 9, 2014 at 20:27

3 Answers 3

1

I think you want something like:

for name in found:
 print(name, useraccounts[name]['totalscore'])
answered Feb 9, 2014 at 20:29
Sign up to request clarification or add additional context in comments.

Comments

0

You can build on what you had before:

scores = [useraccounts[k]['totalscore'] for k in useraccounts if useraccounts[k]['class'] == '12']
# scores = [ list of scores ]
answered Feb 9, 2014 at 20:38

Comments

0

If you only want the totalscore for those specific keys:

totals = [(key,value['totalscore') for key, value in useraccounts.items() if value['class'] == '12']
answered Feb 9, 2014 at 21:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.