Milan_Thapa
Milan_Thapa

Reputation: 59

I am working with python3, where i have list of dictionaries

I have following list of dictionaries which contains job_name,count and city name

question = [{
        "count": 2,
        "city": "Pune",
        "job_NAME": "clerk"
    }, {
        "count": 1,
        "city": "kolkata",
        "job_NAME": "manager"
    }, {
        "count": 2,
        "city": "Mumbai",
        "job_NAME": "accountant"
    }, {
        "count": 1,
        "city": "New Delhi",
        "job_NAME": "clerk"
    }]

what i want is if job_name is same in this case "clerk" the count and city has to be in one list under key "jobs"

   answer = [{
    "job_name": "clerk",
    "jobs": [{
        "city": "pune",
        "count": 2
    }, {
        "city": "New Delhi",
        "count": 1
    }]
}, {
    "job_name": "manager",
    "jobs": [{
        "city": "kolkata",
        "count": 1
    }]
}, {
    "job_name": "accountant",
    "jobs": [{
        "city": "Mumbai",
        "count": 2
    }]
}]

if there is any job duplicate job profile in question list in this case clerk its city and count should be appended in "jobs" key list.

i would be very grateful for any help provided!

Thanks in advance

Upvotes: 1

Views: 80

Answers (3)

rocksportrocker
rocksportrocker

Reputation: 7419

I use defaultdict(list) to group the entries from the input list first and then in a second iteration I setup the result:

from collections import defaultdict

groups = defaultdict(list)

for jobs in question:
    job_name = jobs["job_NAME"]
    del jobs["job_NAME"]
    groups[job_name].append(jobs)

answer = [
    dict(job_name=job_name, jobs=jobs) for job_name, jobs in groups.items()
]

# alternative with classic loop
answer = []
for job_name, jobs in groups.items():
    answer.append(dict(job_name=job_name, jobs=jobs))

Upvotes: 1

Lev Zakharov
Lev Zakharov

Reputation: 2427

One-liner:

from itertools import groupby
from operator import itemgetter

answer = [{'job_name': k, 'jobs': [{f: v for f, v in d.items() if f != 'job_NAME'} for d in g]} 
          for k, g in groupby(sorted(question, key=itemgetter('job_NAME')), key=itemgetter('job_NAME'))]

Result:

[{'job_name': 'accountant', 'jobs': [{'count': 2, 'city': 'Mumbai'}]},
 {'job_name': 'clerk',
  'jobs': [{'count': 2, 'city': 'Pune'}, {'count': 1, 'city': 'New Delhi'}]},
 {'job_name': 'manager', 'jobs': [{'count': 1, 'city': 'kolkata'}]}]

Sort and group question by job_NAME and then just construct answer as list of dicts with job_name and jobs.

Upvotes: 0

Rakesh
Rakesh

Reputation: 82785

Using collections.defaultdict

Ex:

from collections import defaultdict

d = defaultdict(list)
question = [{'count': 2, 'city': 'Pune', 'job_NAME': 'clerk'}, {'count': 1, 'city': 'kolkata', 'job_NAME': 'manager'}, {'count': 2, 'city': 'Mumbai', 'job_NAME': 'accountant'}, {'count': 1, 'city': 'New Delhi', 'job_NAME': 'clerk'}]

for i in question:
    d[i["job_NAME"]].append({"city": i["city"], "count": i["count"]})

answer = [{"job_name": k, "jobs": v} for k, v in d.items()]
print(answer)

Output:

[{'job_name': 'clerk',
  'jobs': [{'city': 'Pune', 'count': 2}, {'city': 'New Delhi', 'count': 1}]},
 {'job_name': 'accountant', 'jobs': [{'city': 'Mumbai', 'count': 2}]},
 {'job_name': 'manager', 'jobs': [{'city': 'kolkata', 'count': 1}]}]

Upvotes: 1

Related Questions