How to not get duplicate letters?

Hi, for the second part of this exercise it seems to makes no relation to string_two under the function common_letters. How is that so?
deeper explanation about the whole part 2 of this exercise is much appreciated.


We only need to iterate over one of the strings since we are not looking for uniques in each string, which would mean iterating both strings. We are looking for letters in common.

Given a letter (or character) from the one string, we use in to see if it is in the other string. If not, go to the next letter. If it does, append the letter to the return list only if it is NOT present already.


I don’t understand how not (letter in common) works in this function.

common =
for letter in string_one:
if (letter in string_two) and not (letter in common)

For example, when letter is break down as b, a,n,a,n,a in the (letter in string_one), and in the (letter in string_two), it tries to match up the letter, and will produce [a,a,a]. But I think common has a empty set, and how could it act to remove the duplicate? Or I want to say that how the variable common works in this case. Please advice.

1 Like

not (letter in common) will be True if letter is not found in the common list. The same can be said of this…

 letter not in common

which does not require brackets. NOT will negate a boolean, so not True will be False, and not False will be True.

1 Like

Thank you. Very helpful.

1 Like

You are not the only one. The “and not” really threw me off. I don’t think it was ever mentioned before. It’s good to know what it means now though.

So what does it mean? Describe it for us. We can make this a learning moment.

Oh I just didn’t know we could use “and not” to combine two conditions in an if statement. If it was mentioned in the Hint section or somewhere in the IF lesson, that’d be great. Thanks for your prompt response!

Understood. Let’s walk back a little to where we only know about if and nothing about and or not.

if condition:
    # condtion is truthy

if is like an operator that works on its argument. Given any object or expression it will behave the same. Evaluate this in terms of truthiness. Nothing that isn’t truthy will get past this point and will be shunted off to the next claus (elif, else) or the program statement following.

Logic is all about truth factors. There are only two, True and, False. That’s it. Every object can be boiled down to one of these two. Call them cases if you will; I do for variation in thinking about logic.

it is going to rain tonight

Is that truthy, or falsy? Other factors may come into play. Are there clouds on the horizon? Is it forty below zero outside (only thing reining (pun) is cold)?

The above may well prove out, but it is only an assertion until then.

it rained last night

Is that truthy or falsy? Assuming it did rain, we would take it as truthy.

if it rained last night:
    the assertion was correct

But in cases where there is doubt, we always need another avenue of testing assertions.

Logical operators are a tool we use for this purpose (among others, perhaps).

if prediction of rain AND temperature suitable AND cloudy:
    expect rain

Logic is about judgement and determination as opposed to imperative.

if prediction:
    expect rain

AND is an operator that short-circuits if it encounters a falsy. The whole thing just shuts down and hands back, False.

That’s the end of the evaluation. This tells us that if we have condition that is likely to fail, then put it last and let the others have a chance first, to succeed if they may.

x = []
if len(x) and x[0]

This will not raise an exception because the doable operation came first. The second operand will never be evaluated.

x = []
if x[0] and len(x)

Will toss up an exception so quick your hands won’t have left the keys yet.

The operator is not blame here. The programmer is.

x = [1]
if not x[0] and len(x)

What will that do?

Or this,

if len(x) and not x[0]

Bottom line, go down this rabbit hole and don’t come back up until you are dreaming about logical operators in your sleep, literally.

Arm yourself with simple truth tables for each of AND, OR, NOT.

Bear in mind that AND and OR are operators whereas NOT is a modifier. Operators are binary, modifiers are unary. They prefix and sometimes suffix a singular adjacent object.

not a

First off, whatever a evaluates as, truthy or falsy will become a boolean. The next thing to happen will be toggling that boolean. Given we have only two factors, that is simple enough. True becomes False; False becomes True. Hardly any evaluation process here. It is imperative. But the evaluation of a was determinative. We had to squeeze it to see what came out.


Wow thank you!!

I’m not sure if you work for Codecademy. If you do, do you know if the company will:

  • update the content for the Python courses where the prerequisite is Python 2? Do those courses use Python 2 instead of Python 3?
  • launch more Python courses for Pro membership in the future?
1 Like

Not CC staff; we are volunteers on this side. Contact your advisor or someone on the support team for reliable information with regard to upcoming courses. All we know is that Python 2 will be sunsetting next year, but the content here will remain for a good long time afterward Whether or not any deficiencies will be remedied, one cannot say.


oh god me too, im feeling so dumb for not understanding this… i have to look at almost all the anwers, im feeling very disappointed

in this one im having trouble in here :

if (letter in string_two) and not (letter in common):

-why the: " and not letter in common" ?

  • also, why is the first part relevant, i deleted and got the same answer?

thank you

not (letter in common) will be True if letter is not found in the common list. We don’t want any duplicate items in the common list, hence checking first, before appending.


Hi guys. I’m stuck on an exercise. The instruction is:

Write a function called common_letters that takes two arguments, string_one and string_two and then returns a list with all of the letters they have in common.

I require for my code to only return one set of identical letters, not duplicates.

def common_letters(string_one, string_two):
  common = []
  for i in string_one:
    if i in string_two:
  return common
print(common_letters('manhattan', 'san francisco'))

#Output in the terminal 
['a', 'n', 'a', 'a', 'n']

#Required output in terminal

Solved with the following code:

def common_letters(string_one, string_two):
  common = []
  for i in string_one:
    if i in string_two:
      if not i in common:
  return common

This is what i have used to solve this exercise :slight_smile:

def common_letters(string_one,string_two):
a =
for i in string_one:
if i in string_two and i in string_one and i not in a:
return a

Don’t we already know it is in string_one? We are, after all, iterating over that string, and it is the current value being examined.

Just ribbing you, though it is a fair point.

Question: Have you been exposed to list objects, yet?

Yes , I finished few exercises on lists :slight_smile:

Actually, I scrolled to the top of this topic… The lesson is looking for common letters, not common words, so a list is not needed since we are iterating character by character. Common words would be another story. My apologies.

I did this one. it worked.

def common_letters(string_1, string_2):
	common = []
	for i in string_2:
		if i in string_1:
			if i not in common:
	return common

To add on to a previous lesson I tried to move this to a list comprehension. but I have a question about it. in the following example:

def common_letters(string_1, string_2):
	common = []
	common = [i for i in string_2 if i in string_1 if i not in common]
	return common

print(common_letters('manhattan', 'san francisco'))

is this doing everything in the list comprehension before it gets assigned into the var common??