Update a Value in Python
<p>Hello I am trying to write a program that reads a CSV file of different animals of various breeds. Various animals, named differently, can be of the same breed. (Imagine two cats named bob and sam)</p>

<p>The breeds are in one column and the names are in another.</p>

<p>I want to be able to go over all the animals and count the number of animals in each breed.</p>

<p>The code I have so far is like this:</p>

<pre><code>dragon = open('dragons.csv')

breed = {}

for line in dragon:
    row = line.strip().split(',')
    if row[4] in breed.keys():

else:
    breed[row[4]] = 1 #The Breed is in the fourth column
</code></pre>

<p>How can I do this?</p>

<p>Thank you!</p>

<p>As the comments point out, indentation matters in python. The <code>else</code> is not on the same indentation level as the <code>if</code> and thus you get an error.</p>

<p>as for the counting, the <code>+=</code> operator is useful for that, so your if/else block could be like this:</p>

<pre><code>if row[4] in breed.keys():
    breed[row[4]] += 1
else:
    breed[row[4]] = 1
</code></pre>

python arrays csv dictionary