Help Understanding Solution to - Strings and Conditionals (Part Two)

Hi CodeCademy community, I’m having a hard time understanding the solution here, specifically

and not (letter in common):

Here’s the full solution code:

def common_letters(string_one, string_two):
  common = []
  for letter in string_one:
    if (letter in string_two) and not (letter in common):
      common.append(letter)
  print(common)
  return common


common_letters("manhattan", "san francisco")

The way I read this is that we create an empty list to house all our similar letters, then we start a for loop in string_one looking through each letter and starting an if then statement where we check if the “letter” from string_one was in string_two, but not in our newly created empty list.

Am I reading that correctly? Why is the “not” statement crucial to running this code?

Link to exercise

@trevor.aaron welcome. You are reading that correctly.
The not is crucial to the operation of the code because without it, there you would get double ups. For instance, if the string_1 were “Apple and apples” and the string_2 were “Apricots and others”, the common list would contain 2 a, because there are two of the letter a in common. But, in this situation, you only want to know if there are letters in common, not how many there are. If that makes sense. I hope this helps!

2 Likes