#🔒 sorting nested dictionary

42 messages · Page 1 of 1 (latest)

thick gale
#

I have a dictionary where the first element is the continents.
'''
{asia : {japan : [cities], china: [cities]},

}

'''
I want to sort it by the countries, so japan and china. the commented out code is currently what I have. I don't really don't know how to proceed here.

short swiftBOT
#

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.
short swiftBOT
#

@thick gale

Python help channel opened

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.

feral tiger
#

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...?

thick gale
#

I sort the contients so it goes africa, asia then europe, then I want to sort the countries and then the cities

thick gale
#

this is the current error I get with what I'm trying to do

neon vigil
#

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

thick gale
#

oh so by putting dict() on it, it's trying to force it to be a dict data structure

neon vigil
#

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.

thick gale
#

so for now just do sorted(data_types[continents].keys())

neon vigil
#

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?

thick gale
#

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.

neon vigil
#

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)

short swiftBOT
#

@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}
neon vigil
#

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)

short swiftBOT
#

@neon vigil :white_check_mark: Your 3.12 eval job has completed with return code 0.

{'apple': 4, 'banana': 3, 'orange': 2, 'pear': 1}
thick gale
#

are you talking about line 57

neon vigil
#

I was only looking at your error line 60

thick gale
#

oh okay. yeah that doesn't return a dict, it's giving me a type error unhashable type: list

neon vigil
#

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

thick gale
#

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

short swiftBOT
#
Python help channel closed

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.