title: Python- How to return a dictionary that counts occurrences in a list of strings?
body: <p>I'm trying to make a function that counts occurrences of the first letters of a list of strings and returns them as a dictionary.</p>
  <p>For example:</p>
  <blockquote>
  <p>list=[&quot;banana&quot;,&quot;ball&quot;, &quot;cat&quot;, &quot;hat&quot;]</p>
  <p>dictionary would look like: {b:2, c:1, h:1}</p>
  </blockquote>
  <p>Here is the code I have which iterates but doesn't count properly. That's where I'm getting stuck. How do I update the values to be count?</p>
  <pre><code>def count_starts(text):
      new_list=[]
      for word in range(len(text)):
          for letter in text[word]:
              if letter[0]=='':
                  new_list.append(None)
              else:
                  new_list.append(letter[0])
              
      new_dict= {x:new_list.count(x) for x in new_list}
  
  
      return new_dict
  </code></pre>
  <p>Also, how can I avoid the out of range error given the following format:</p>
  <pre><code>def count_starts(text):
      import collections
      c=collections.Counter(x[0] for x in text)
      return c
  </code></pre>
  <p>Also, what do I need to do if the list contains &quot;None&quot; as a value? I need to count None.</p>