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.

3 Answers 3

1

I think you want something like:

for name in found: print(name, useraccounts[name]['totalscore']) 
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 ] 

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'] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.