-2

So. I'm trying to code a program where the user inputs a list of letters i.e.

l=['a','a','a','a','b','b','r',]

and a dictionary is generated where the keys are each letter, and the values are how many occurrences the letter has in the list, i.e:

dictionary1={a:4, b:2, r:1, z:0}

Here's what I've tried so far

def list1(l):
  
  dictionary1 = {}
  
  
  for x in l:
    dictionary1[x]:l.count(x)
  
  return dictionary1

But the output comes as an empty dictionary. How should I proceed? Thanks in advance

ggorlen
  • 44,755
  • 7
  • 76
  • 106

1 Answers1

2

most simple functionality like that is already included in python. For example, this is the canonical solution:

from collections import Counter

l=['a','a','a','a','b','b','r',]
dictionary1 = Counter(l)
anon01
  • 10,618
  • 8
  • 35
  • 58