+ 1

Help Understanding Intermediate Python Course Code Project Please: Letter Counter

Hello, I really struggled with the above code project and spent hours going back through the lessons and trying to come up with a program that worked. I failed miserably, which is okay because failure is often the first step to knowledge. So, to my shame and annoyance, I searched the internet for code that works with the intention of understanding why the code works and then writing my own program but only using the information and tools provided by SoloLearn in the Intermediate and Beginners Python courses. However, I don't understand what the code that I have taken (and amended!) from Stack Overflow is doing and why it works. If anyone is kind enough to explain to me how the code works, I'd be really thankful. I have provided a description of the code project below along with the code. I'm specifically confused by 'counts.get(char, 0) + 1', the rest of the code I can kind of make sense of, for example, 'counts[char]' is populating the dictionary with keys that are the individual characters of the input string. Description of task: Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input hello Sample Output {'h': 1, 'e': 1, 'l': 2, 'o': 1} Amended Stack Overflow code: text = input() dict = {} def counter(text): counts = dict for char in text: counts[char] = counts.get(char, 0) + 1 return counts print(counter(text)) Thank you in advance for taking the time to help.

11th May 2022, 10:55 AM
Marc Sharp
Marc  Sharp - avatar
7 Answers
+ 3
Marc Sharp Let's understand through this example step by step: #example :- anant #1st iteration, counts.get(char, 0) + 1 = 0 + 1 = 1 #now counts = {'a' : 1} #2nd iteration, counts.get(char, 0) + 1 = 0 + 1 = 1 #now counts = {'n' : 1} #3rd iteration, counts.get(char, 0) + 1 = 1 + 1 = 2 #now counts = {'a' : 2} #4th iteration, counts.get(char, 0) + 1 = 1 + 1 = 2 #now counts = {'n' : 2} #5th iteration, counts.get(char, 0) + 1 = 0 + 1 = 1 #now counts = {'t' : 1} #now finally counts = {'a' : 1, 'n' : 1, 'a' : 2, 'n' : 2, 't' : 1} #as dictionary cannot contains duplicate so first key will be override by next key #so finally counts = {'a' : 2, 'n' : 2, 't' : 1}
11th May 2022, 11:10 AM
A͢J
A͢J - avatar
+ 3
Marc Sharp "key value with the highest count" Not with the highest count, key will be replaced by second duplicate key: counts = {'a' : 2, 'a' : 1} print (counts) # {'a' : 1}
12th May 2022, 8:19 AM
A͢J
A͢J - avatar
+ 2
counts.get(char, 0) returns value at key 'char'. If there is no key with 'char' then it returns 0 (default value).. counts.get(char) returns value at key 'char' but if there is no key in dict counts then it raise error.. returned value + 1 is assigned in counts[char]. Hope it helps..
11th May 2022, 11:06 AM
Jayakrishna 🇮🇳
+ 2
Marc Sharp Its just indexing like list counts[char] = 1 is in dict counts at index 'char' setting value to 1. And indexing approach counts[char] returns value st index 'char'. It's same as counts.get(char)
11th May 2022, 12:09 PM
Jayakrishna 🇮🇳
+ 1
Thanks very much for your help! I think have misunderstood what counts[char] is doing, as it looks like counts.get(char, 0) + 1 is populating the dictionary with the key and the value, where as I thought counts[char] populates the dictionary with the keys and counts.get(char, 0) + 1 populated the dictionary with the values. What is counts[char] doing? This syntax is not explained in the preceding lessons, as I have looked for an example where a dictionary has [ ] after it and I can't find one. The [ ] looks like some kind of comprehension, as with lists, but I'm not sure. Again, my sincere thanks for your support with this.
11th May 2022, 11:54 AM
Marc Sharp
Marc  Sharp - avatar
+ 1
Okay. I finally understand. Thanks again for your examples and explainations. Super helpful! The counts[char] = counts.get(char, 0) is actually deducible from the lesson content. What isn’t clear, and where the confusion comes into it for me, is what the +1 is doing and how it’s doing it. If I’ve understood this correctly, counts[char] = counts.get(char, 0) + 1 is populating the dictionary with the characters from the input text as ‘keys’ on each iteration of the ‘for’ loop and populating an increment of +1 as the corresponding value for each instance of the ‘key’ character found in the input text. As explained by AJ in his examples, the first instance of a letter, say ‘a’, will be populated in the dictionary as {‘a’ : 1}. Then the second instance of the letter ‘a’ will be populated in the dictionary as {‘a’ : 2}. Dictionaries cannot contain duplicates, so the key value pair with the highest count remains when the dictionary is printed to console. Thanks again.
12th May 2022, 7:50 AM
Marc Sharp
Marc  Sharp - avatar
+ 1
Ah, that’s great! Thanks again AJ!
12th May 2022, 8:50 AM
Marc Sharp
Marc  Sharp - avatar