Python Dictionary: compare 2 values in 1 key
<p>I have a dataset where there is a list of names in a column, and a response for each name in a separate column. Each name is listed twice, and I want to see if there is agreement between the two recorded responses. 
i.e.</p>

<p>name a | response 1</p>

<p>name a | response 2</p>

<p>name b | response 1</p>

<p>name b | response 2</p>

<p>I created a dictionary where the key has two values. The dictionary creates the name as the key, and each response as a value. I want to create a list to see if response1 = response2, or if response1 != response2. Here is what I have so far:</p>

<pre><code>myDict = {}
if name not in myDict.keys():
    myDict[name] = {'response1': answer}
else:
    myDict[name]['reponse2'] = answer

match = True
for items in hospitalDict:
    if hospitalDict[items] != hospitalDict[items]:
          match = False
print match
</code></pre>

<p>I am stuck on this part...any advice on how to construct this? I would also like to output this data to a csv eventually.</p>

<p>I implemented NamesDict class, which could have 2 or more responses for each key (name), compare it and export. To export in csv you can easily use csv library for python: <a href="https://docs.python.org/2/library/csv.html" rel="nofollow">https://docs.python.org/2/library/csv.html</a></p>

<p>I put comparison and csv export functions as an example.</p>

<pre><code>import csv


class NamesDict(dict):
    def __init__(self, *args, **kwargs):
        super(NamesDict, self).__init__(*args, **kwargs)

    def __setitem__(self, key, item):
        if isinstance(item, dict):
            self.__dict__[key] = item
        else:
            raise Exception('item has to be a dict')

    def __getitem__(self, key):
        return self.__dict__[key]

    def responses_match(self, key):
        # Here you can implement your own comparison method
        match = False
        for key_one, value_one in self.__dict__[key].items():
            for key_two, value_two in self.__dict__[key].items():
                if key_one != key_two and value_one == value_two:
                    match = True
        return match

    def export_csv(self, path):
        # Here you can change csv export
        with open(path, 'wb') as csv_file:
            fieldnames = ['name', 'responses']
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            for key, value in self.__dict__.items():
                responses_string = ''
                for resp_key, resp in value.items():
                    responses_string += '%s=%s\n' % (str(resp_key), str(resp_key))

                writer.writerow({
                    'name': key,
                    'responses': responses_string
                })


if __name__ == '__main__':
    namesDict = NamesDict()
    namesDict['test'] = {
        'response1': 1,
        'response2': 2
    }

    namesDict['test2'] = {
        'response1': 2,
        'response2': 2
    }

    print(namesDict.responses_match('test'))    # False
    print(namesDict.responses_match('test2'))   # True

    namesDict.export_csv('test.csv')
</code></pre>

python python-2.7 dictionary key