Some coding practice with Json, Itertools and Lambda in Python
By Fahad Ahammed
- One minute read - 188 wordsI enjoy coding and solving problems. But as I go forward, I find myself in a sea of unknowns. Thus I decided to learn as much as possible to be confident. So, I am learning even the simplest it seems.
Let me start.
I have a json file named data.json having data –
How do I read this file in Python?
This code will just open the file and make the contents as a python dictionary when loaded by json.load().
Now, Let I want to get a dictionary from that list of dictionary with countries as the key and all the persons of a particular country will be in a list of that key. As an example –
How can I do that?
I ran a loop and checked some conditions to make a dictionary. Now it can be done by another way.
Here, I have used groupby function from itertools and also little bit lambda to sort and other key.
Obviously It can be done with many ways and can be optimized. I will try to update this post when I again have time working on this.