Introduction to Strings - Strings and Conditionals (Part Two) Section 11 - Why doesn't this work?

https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-python-strings/lessons/introduction-to-strings/exercises/strings-in-conditionals-ii

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.

So, I brain-farted and attempted to create a solution completely unrelated to the lesson. Now I could just shrug and just use the far-simpler solution, but I want to understand why the code below simply doesn’t work and returns an empty list instead of filling up with unique letters. So…why doesn’t the common_list report unique letters and returns an empty list?

def common_letters(string_one, string_two): common_list = [] if len(string_one) > len(string_two): # ensures that I use every single letter in string_one if it is longer than string_two for string_one_letter in string_one: #goes through every single letter in string one for string_two_letter in string_two: #goes through every single letter in string two if string_one_letter == string_two_letter: #checks if the string_one_letter matches with the string_two_letter for element in common_list: #goes every element in the common_list if string_one_letter != element: #checks if the string_one_letter does not match any element in the common list common_list.append(string_one_letter) #if the string_one_letter doesn't match any element in the common list, add the string_one_letter into the common list return common_list else: # if string_two is actually longer than string_one, this is the contingency for that for string_two_letter in string_two: for string_one_letter in string_one: if string_two_letter == string_one_letter: for element in common_list: if string_one_letter != element: common_list.append(string_one_letter) return common_list

I think there are a lot of issues with the logic of the code and it is hard to follow so many nested loops.
But as for why common_list is an empty list, you initialize common_list as an empty list. In your code, appending to this list only happens when going through the loop

for element in common_list:

But common_list is empty and so the above for loop doesn’t run, because there is no element to iterate over. And since, appends to common_list only happen within this loop, so common_list never changes and remains an empty list.