Some coding practice with Json, Itertools and Lambda in Python

I 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 –

[
  {
    "name": "Guido Van Rossum",
    "city": "Haarlem",
    "country": "Netherlands",
    "known_for": "Python"
  },
  {
    "name": "Tim Berners-Lee",
    "city": "London",
    "country": "United Kingdom",
    "known_for": "HTML"
  },
  {
    "name": "Brendan Eich",
    "city": "Pittsburgh",
    "country": "United States of America",
    "known_for": "Javascrip"
  },
  {
    "name": "Yukihiro Matsumoto",
    "city": "Osaka",
    "country": "Japan",
    "known_for": "Ruby"
  },
  {
    "name": "James Gosling",
    "city": "Calgary",
    "country": "Canada",
    "known_for": "Java"
  },
  {
    "name": "Linus Torvalds",
    "city": "Helsinki",
    "country": "Finland",
    "known_for": "Linux"
  },
  {
    "name": "Håkon Wium Lie",
    "city": "Halden",
    "country": "Norway",
    "known_for": "C++"
  },
  {
    "name": "Rasmus Lerdorf",
    "city": "Qeqertarsuaq",
    "country": "Denmark",
    "known_for": "php"
  },
  {
    "name": "Dennis Ritchie",
    "city": "Bronxville",
    "country": "United States of America",
    "known_for": "C"
  },
  {
    "name": "Bjarne Stroustrup",
    "city": "Aarhus",
    "country": "Denmark",
    "known_for": "C++"
  }
]

How do I read this file in Python?

import json

with open(file="data.json", mode="r") as opened_file:
    json_file_object = json.load(opened_file)

print(json_file_object)

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 –

{
  'United Kingdom': [
    {
      'name': 'Tim Berners-Lee',
      'city': 'London',
      'country': 'United Kingdom',
      'known_for': 'HTML'
    }
  ],
  'United States of America': [
    {
      'name': 'Brendan Eich',
      'city': 'Pittsburgh',
      'country': 'United States of America',
      'known_for': 'Javascript'
    },
    {
      'name': 'Dennis Ritchie',
      'city': 'Bronxville',
      'country': 'United States of America',
      'known_for': 'C'
    }
  ]
}

How can I do that?

import json

with open(file="data.json", mode="r") as opened_file:
    json_file_object = json.load(opened_file)


persons = json_file_object

new_persons = persons.copy()

return_obj_filter = {}
for i, v in enumerate(persons):
    commons = list(filter(lambda person: person["country"] == v["country"], new_persons))
    if commons:
        if return_obj_filter.get(v["country"]):
            return_obj_filter[v["country"]] = return_obj_filter[v["country"]].append(commons)
        else:
            return_obj_filter[v["country"]] = commons

        for ii in commons:
            indx = new_persons.index(ii)
            new_persons.pop(indx)
            
            
            
print(return_obj_filter)

I ran a loop and checked some conditions to make a dictionary. Now it can be done by another way.

import json
from itertools import groupby

with open(file="data.json", mode="r") as opened_file:
    json_file_object = json.load(opened_file)


persons = json_file_object

persons.sort(key=lambda x: x["country"])
gob = groupby(persons, key=lambda xx: xx["country"])
return_obj_gob = {}
for k, v in gob:
    return_obj_gob[k] = list(v)
print(return_obj_gob)

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.