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 assume what you want to achieve is a list of names that have matching answers.</p>

<p>given that you have the dictionary already created:</p>

<pre><code>myDict = {
    'name 1' : {'response1':'A', 'response2':'B'},
    'name 2' : {'response1':'C', 'response2':'C'},
    ... ,
    'name N' : {'response1':'Z', 'response2':'Z'},
}
</code></pre>

<p>you could do the following:</p>

<pre><code>myList = []
for name, resp in myDict:
    if resp['response1'] == resp['response2']:
        myList.append(name)
print myList
</code></pre>

<p>the result should be something like:</p>

<pre><code>['name 2', ..., 'name N']
</code></pre>

<p>that list only includes names with matching answers</p>

python python-2.7 dictionary key