Function returning a dictionary with string lengths (keys) and number of occurrence (values) from a list
I am trying to write a function that takes a list of strings and returns a dictionary where the keys represent a length and the values represent how many strings have that length in the list. This is what I have written and my output: def length_counts(stringList): stringDict = {} for value in stringList: stringDict[len(value)] = stringList.count(value) return stringDict #test sa_countries = ["Brazil", "Venezuela", "Argentina", "Ecuador", "Bolivia", "Peru"] print(length_counts(sa_countries)) #output {6: 1, 9: 1, 7: 1, 4: 1} Correct output should be: {6: 1, 9: 2, 7: 2, 4: 1} Which says there is 1 string with 6 letters, 2 strings with 9 letters, 2 strings with 7 letters, and 1 string with 4 letters. Thank you.