Python Code Challenges II - Strings - Count Letters

Hi folks, Im suffering from a Logic Failure in my own brain.

I attempted and successfully completed the first coding challenge with the following code:

def unique_english_letters(word):
  count = 0
  unique = []
  for letter in word:
    if letter in letters and letter not in unique:
      count +=1
  return count

My logical process was this: I need to create a counter to increment for each UNIQUE letter found in a string. I then need to create a list for UNIQUE letters.
I then need to create a loop to iterate through each letter which appears in the string provided by ‘word’. Within that loop, i will check if the current letter appears in the string letters (containing all english letters) AND ensute the letter does not appear in the list of UNIQUE letters defined previously. If both conditions are met, the count is increased and the letter is added to the unique list of letters. This provides the appropriate result (4 and 4 for mississippi and apple)

So, logically, in my mind this made sence and my code followed my brain path.

However, i scroll down a little and find the solution provided is ultimately simpler, yet less logical to my brain:

  unique = 0
  for letter in letters:
    if letter in word:
      unique +=1
  return unique 

So in this i see we set the counter as 0, then iterate through each letter in the predefined letters string. Within this loop we check whether the current letter appears in the string defined by ‘word’ and if so, we add 1 to the count and return the count after the loop is completed.

I cant 100% wrap my head around it, but in writing this post i think i understand.

Am i right in saying that in the second example, we are looping through the letters defined in the english alphabet string, then checking if a single ‘letter’ in the ‘word’ provided matches that condition - therefore, it will only provide unique instances of the letter appearing in word?

Im sorry if this does not make much sense. My brain works in a strange way at times, so im just looking for a sanity check more than anything.

I feel like i am so close to grasping this 90% of the time, but then some logic trips me up and perhaps i overcomplicate the matter.

Anyway, thanks in advance, sorry if i sound stupid and cheers for the awesome posts you guys make in the forums, super helpful to me

1 Like

You accomplish the same thing but with some redundancy. The append to the unique list is unnecessary but I completely see the confusion. How do we know that the +1 to count is unique? That’s why you created the list to check against the added character.

The solution iterates through the Letters List rather than the Input Word.
So by going through Letters, you compare it to the Word, rather than the other way around.

Therefore if a letter is in the word, it’ll add 1, and since you’re iterating through the individual letters of the Letters List(containing both upper and lowercase), they will be unique since they only appear once.

By comparing Word to Letter List, you are right in appending to a new list and checking to see if it’s unique since every letter in Word will be in the Letter List.

Hope that makes sense!! Answering this really helped me out


Ah! yes, that is what i was missing in my understanding there. So many ways to consider the same problem! Thank you for taking the time to explain.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.