Skip to content
Advertisement

How to group a json by a nested key using Python?

Lets say we have a json object in Python:

JavaScript

What is the most efficient way to group this data by city, so that we end up with a json in which we group the data by city such that we end up with a json as:

JavaScript

… in which the content of the people are included in “people” key.

Thanks!

Advertisement

Answer

Try:

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement