#๐ Help with sorting(and deleting) in dictionary
18 messages ยท Page 1 of 1 (latest)
@foggy plaza
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
You can delete from dictionaries using the del keyword.
That is what I am trying to figure out. Where and how to put it
!e
d = { 'a': 1, 'b': 2 }
del d['b']
print(d)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
{'a': 1}
I'd have to put the nested keys in there and I have around 10 of them and once I do, it also breaks the whole thing
If you want to delete the whole subdict, no need to empty the nested dict first.
Also, sometimes it's better to re-create a dict with less keys (basically make a copy but with only some keys) than to play with removing the keys.
What I'm trying to create here is a sort of ranking list.
Like:
Science:
Students 1-10
Math:
Students 1-10
History:
Students 1-10
It has to be 10 students and 3 subjects specifically
Do you mean you just want to limit what gets printed out when showing the sorted rankings?
Yes
You could just map over sd to reformat the data.
Like:
!e
sd = [('Name1', {'History': 10, 'Math': 20}), ('Name2', {'History': 30, 'Math': 40})]
formatted = [f"{name}: {grades['Math']}" for (name, grades) in sd]
print(formatted)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
['Name1: 20', 'Name2: 40']
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.