0
info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}

error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

I have two dictionaries with username as keys. How can I merge them so that the key remains the same and both the values are assigned to the key? if the key is not present in any one of the dictionary, it should assign value 0. Actually I need to create a CSV file from the above dictionaries in the following format. "USERNAME","INFO","ERROR". Any help is appreciated. The values must not be added.

Drazer
  • 67
  • 6
  • Does this answer your question? [Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?](https://stackoverflow.com/questions/11011756/is-there-any-pythonic-way-to-combine-two-dicts-adding-values-for-keys-that-appe) – r.ook May 14 '20 at 19:25

5 Answers5

3
info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}
error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

from csv import DictWriter

with open('output.csv', 'w') as f:
    fields = ['USERNAME', 'INFO', 'ERROR']
    writer = DictWriter(f, fields)
    writer.writeheader()
    all_users = list(info_dict.keys())
    all_users.extend(error_dict.keys())
    for user in sorted(set(all_users)):
        writer.writerow({
            'USERNAME': user,
            'INFO': info_dict.get(user, 0),
            'ERROR': error_dict.get(user, 0)
        })
Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
2

The easiest way to do this would be to get a list of all the keys in both the dictionaries and retrieve these values. In this case I have assumed the resultant dictionary such that each name is a key in a dictionary and each values is a dict of two keys info and error with their respective values (or 0 if it doesnt exist).

Anyways, the code for the same:

info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}

error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

result_dict = {}

for name in set(list(info_dict.keys())+list(error_dict.keys())):
    result_dict[name] = {
        "info": info_dict.get(name, 0),
        "error": info_dict.get(name, 0)
        }

print(result_dict)
Samay Gupta
  • 437
  • 3
  • 8
  • Thank you very much. One of my issues got solved with this but I'm yet to figure out how to create a CSV file in the format (USERNAME, INFO, ERROR). Any help in this regard if u can is highly appreciated. – Drazer May 14 '20 at 19:39
2

Here's a version that creates a list of lists as output and uses pandas to export a csv as you requested.

import pandas as pd

info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}
error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

every_name.sort()

output = []
for name in set(list(info_dict.keys())+list(error_dict.keys())):
    info = info_dict.get(name,0)
    error = error_dict.get(name,0)

    output.append([name, info, error])

df = pd.DataFrame.from_records(output, columns=["username", "info", "errors"])
df.to_csv('output.csv')
Matt L.
  • 3,431
  • 1
  • 15
  • 28
1

Something like this?

info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}
error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

new_dict = {key: [info_dict.get(key, 0), error_dict.get(key, 0)]
            for key in set(info_dict.keys()).intersection(error_dict.keys())}

print(new_dict)

Which yields

{'kirknixon': [2, 4], 'rr.robinson': [1, 3], 'ac': [3, 4], 
'jackowens': [4, 4], 'mdouglas': [5, 2], 'mcintosh': [4, 7], 
'blossom': [11, 3], 'breee': [9, 1], 'sri': [3, 4], 
'enim.non': [5, 4], 'britanni': [1, 1], 'ahmed.miller': [6, 3], 
'noel': [5, 10], 'oren': [9, 3], 'nonummy': [5, 3]}
Jan
  • 42,290
  • 8
  • 54
  • 79
1

Something like:

info_dict = {'breee': 9, 'ac': 3, 'mcintosh': 4, 'jackowens': 4, 'mdouglas': 5, 'oren': 9, 'xlg': 6, 'ahmed.miller': 6, 'blossom': 11, 'bpacheco': 3, 'enim.non': 5, 'rr.robinson': 1, 'flavia': 6, 'sri': 3, 'nonummy': 5, 'montanap': 6, 'mai.hendrix': 6, 'kirknixon': 2, 'noel': 5, 'britanni': 1}

error_dict = {'mdouglas': 2, 'noel': 10, 'blossom': 3, 'rr.robinson': 3, 'breee': 1, 'nonummy': 3, 'ahmed.miller': 3, 'jackowens': 4, 'ac': 4, 'kirknixon': 4, 'sri': 4, 'enim.non': 4, 'mcintosh': 7, 'oren': 3, 'britanni': 1}

users = dict()
for user in info_dict.keys():
    if user in error_dict:
        users[user] = {'info':info_dict[user],'error':error_dict[user]}
    else:
        users[user] = {'info':info_dict[user],'error':0}

for user in  error_dict.keys():
    if user in users:
        continue
    users[user] = {'info':0,'error':error_dict[user]}

print (users)
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20