How to not get duplicate letters?

if list.count(i) == 0:

I’m curious about spacing here. I did the following:

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)
      return common

print(common_letters("Manhattan", "San Francisco"))

Returns [“a”]

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)
  return common
print(common_letters("Manhattan", "San Francisco"))

Returns [“a, n”]
Why does that extra space in the first return change the code? I’ve learned that incorrect spacing will throw errors and depending on spacing the code will be part of a function or be separate from it. In the second function the return statement is part of the code. Indented the same amount as for. In the first one I played with it some and got it to return and “a” and throw an error. Why is the position changing the outcome?

Only problem here is that without code sample markdown we cannot see how your code is indented. If you can still edit the post, Select each code sample and click the </> button beside the quote in the toolbar.

1 Like

Awesome thanks for the editing help.

1 Like

That return is in the if statement, and in the loop, neither of which should be the case. Notice where the return is in the second example?

Same place as the for statement.
Not in the if statement. So in the first one I moved it around some and got it to return an empty string and ‘a’. That’s because it’s acting as part of the if statement. Got it. Thanks.

1 Like

In Python the indentation is critical.

1 Like

same for me with the “and not” dont remember learning about it before but thanks to this forum, I think i understand it now

I’m happy for solving this problem on my own first, if it aligns with the forum’s guideline, I would post the code with explanation:

BTY , it’s my first post in forums :slight_smile:

def common_letters(string_one,string_two): in_common =[] for letter in string_one: # checks letter by letter in 'string_one' if letter in string_two: # condition 1 : compares *if* the letter is included also in 'string_two' if letter not in in_common: # condition 2 : letter should NOT be appended in_common more then one time in_common.append(letter) # if the letter meets both conditions it will be appended return in_common print(common_letters("United States","Russia"))

If you use a dictionary, why not just

def common_letters(string_one, string_two):
  return list( {x: 1 for x in string_one if x in string_two} )

It works, so yeah. I like code to be obvious so let the reader know what is going to be in the list without leaning on how Python casts a list from a dictionary. It is not immediately obvious.

Thanks. Definitely a good approach. I was not sure if I miss something.

1 Like

Thank you mtf, because of your comments i easily performed my second part of the exercise.see you soon for another unstock.

1 Like