#🔒 sorting nested dictionary
42 messages · Page 1 of 1 (latest)
Hey @thick gale!
It looks like you are trying to paste code into this channel.
You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not '''.
Here is an example of how it should look:
```
Hello, world!
```
This will result in the following:
Hello, world!```
You can **edit your original message** to correct your code block.
@thick gale
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.
the asia dict contains the japan and china keys, so you want to sort the keys:
for country_key in sorted(continent.keys()):
this will give you 'china', then 'japan', in your example
it looks like you already knew this, however, since you sort the continents at the start...?
I sort the contients so it goes africa, asia then europe, then I want to sort the countries and then the cities
this is the current error I get with what I'm trying to do
Remove the dict() from that last line and take a look at : what type is returned by sorted there?
(Line 60: Where that error occurs)
The hint is: you’re trying to sort the keys of a dictionary, not the dictionary itself
oh so by putting dict() on it, it's trying to force it to be a dict data structure
The way to debug these problems is to simplify and debug each step.
Much as it’s tempting to write dict(sorted(dict[something].keys())), you have four different things happening here. Do one thing per line, to make it easier to debug.
so for now just do sorted(data_types[continents].keys())
What do you expect that to do?
And, try it: what happens?
There’s a logical problem here: you’re trying to sort the keys… then trying to turn those sorted keys into a dictionary. Do you see why that won’t do what you want?
yeah I see the issue, however what I want is to sort it so that the countries within every continents are sorted. Doesn't have to put it back into the dictionary necessarily, just need it for printing purposes.
Just the country names?
Err sorry, no; you’re trying to sort the continents first.
But you’re not going to get a dictionary back by sorting the keys.
If you want to sort a dictionary, then do something like:
!e example_dict = {"banana": 3, "apple": 4, "pear": 1, "orange": 2}
sorted_keys = sorted(example_dict.keys())
sorted_dict = {key: example_dict[key] for key in sorted_keys}
print(sorted_keys, sorted_dict)
@neon vigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
['apple', 'banana', 'orange', 'pear'] {'apple': 4, 'banana': 3, 'orange': 2, 'pear': 1}
The simpler version is just sorting the dict’s items rather than the keys:
!e example_dict = {"banana": 3, "apple": 4, "pear": 1, "orange": 2}
sorted_dict_one_step = dict(sorted(example_dict.items()))
print(sorted_dict_one_step)
@neon vigil :white_check_mark: Your 3.12 eval job has completed with return code 0.
{'apple': 4, 'banana': 3, 'orange': 2, 'pear': 1}
are you talking about line 57
I was only looking at your error line 60
oh okay. yeah that doesn't return a dict, it's giving me a type error unhashable type: list
Yup, so break that into multiple lines. Don’t fall into the ‘dense is good’ trap.
Then you’ll see the step that’s failing
so data_map[continent].items() returns a list of items.
so I just then need to sort the list so that the first elements are sorted
and then use the list to print
I don't need to modify the dictionary anymore
I think i figured it out
data_map[continent] = dict(sorted(data_map[continent].items()))
I did it layer by layer and that's what I came up with
it returns me a sorted dictionary with the nested layer sorted
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.