Skip to main content

Combine lists into a dictionary whilst also tallying the entries for the second list

I'm new to python and would really appreciate some help.

I have a list of companies and a list of professions and I would like to combine them into a dictionary while also counting how many times the same profession comes up in a given company.

I currently combine them as folows:

info={}
i = iter(company)
j = iter(profession)
k = list(zip(i, j)) 
for (x,y) in k: 
    if type(y) == str:
        if x in info:
            info[x] = info[x],y 
        else:
            info[x] = y    

This produces:

CompanyA: "Engineer", "Group Leader","Administrator","Engineer","Engineer"
CompanyB: "Engineer", "Group Leader","Group Leader","Group Leader","Group Leader",
etc...

But I would like instead of repeating the professions, to have something like:

CompanyA: "Engineer":3, "Group Leader":1,"Administrator":1 
CompanyB: "Engineer":1, "Group Leader":4


source https://stackoverflow.com/questions/76415081/combine-lists-into-a-dictionary-whilst-also-tallying-the-entries-for-the-second

Comments