2 dimensional dictionary in Python

I'm just learning python, and I'm very basic in terms of programming to begin with.

I have a bunch of data that I need to summarize.  I'm retrieving this data from a vendor's billing system via their api and have it in json.

I can query against it and such, and I'm able to dump it to Excel in a CSV; I can summarize it there manually, but would like to automate it.  I'm trying to determine the best way to handle this programmatically, and so far it seems I need a multi-dimensional dictionary, or maybe a dictionary of lists?

Here's a sample of data -

@domain1.com 1 0
@domain1.com 0 0
@domain1.com 1 1
@domain2.com 0 0
@domain3.com 1 1
@domain3.com 0 1

The output I would like to get is -
@domain1.com 2 1
@domain2.com 0 0
@domain3.com 1 2

I'm basically just summing up the 1s and 0s at the end of each domain.

I started down the road of using the defaultdict(list).  As the domains would be the key, I figured I'd just have a dictionary containing each domain, and a list attached to each with the sums of the 2nd and 3rd columns.  But I'm getting a little twisted around in terms of the programming and how it would look. . Suggestions?  Am I going down the right road?  

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Can you share a sample of your json?
diperspAuthor Commented:
Most of it is authentication and such.  Here's the request and the little bit of manipulation I've done so far -

r = requests.get(enterpriseURL + str(masterEnterpriseId) + '/customers', headers=headers)
masterData = r.json()

for clients in masterData:
    r = requests.get(enterpriseURL + str(clients["enterpriseId"]) + '/users', headers=headers)
    clientData = r.json()
    for user in clientData:
        if user["settings"]["securityStatus"] == "ON":
            col1 = 1
            col1 = 0
        col2 = user["services"].count("ARCHIVING")
        writetofile.writerow([domain.group(), col1, col2])

Open in new window

Walter RitzelSenior Software EngineerCommented:
Ok, that is fine. But can you share a sample of the json data loaded by this piece of code? I mean, the content that is stored on the variable at line 6 in your sample code above.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

diperspAuthor Commented:
[ {
  "name" : "User1",
  "userId" : "1691768",
  "enterpriseId" : "850",
  "primaryAddress" : "user1@domain1.com",
  "type" : "MAILBOX",
  "policyGroups" : [ "NORMAL_USER" ],
  "settings" : {
    "securityStatus" : "ON",
  "services" : [ "ARCHIVING" ]
}, {
  "name" : "User2",
  "userId" : "1691828",
  "enterpriseId" : "850",
  "primaryAddress" : "user2@domain2.com",
  "type" : "MAILBOX",
  "policyGroups" : [ "NORMAL_USER" ],
  "settings" : {
    "securityStatus" : "ON",
  "services" : [ "ARCHIVING" ]
} ]

Open in new window

Walter RitzelSenior Software EngineerCommented:
OK, onde last question: domain is extracted from primary address?
diperspAuthor Commented:
Correct - though I see that line is missing in my code.  Too much copying and pasting and changing!  I'm basically doing a regex to pull that.
@dipersp -  the way you described it, what you need is a dictionary of lists. To do that with defaultdict (have to pass constructor function to default dict and the no-argument lambda above returns 2-element list of zeros):
>>> from collections import defaultdict
>>> sumr = defaultdict(lambda: [0, 0])
>>> sumr['domain2.com'][1] += 2
>>> sumr['domain2.com']
[0, 2]

Open in new window

Or we can do that less efficient with dictionary of dictionaries (may give mild headache trying to understand):
>>> sumr = defaultdict(lambda: defaultdict(int))
>>> sumr['domain1.com']['col1'] += 1
>>> sumr['domain1.com']
defaultdict(<type 'int'>, {'col1': 1})
>>> sumr['domain1.com']['col2']

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
diperspAuthor Commented:
This project got yanked for the time being, so never got back to this.  Sorry for letting it hang. Awarding points for the attempt to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.