Please see the below code
def common_letters(string_one,string_two):
letters_in_common = []
for letter in string_one:
for letter2 in string_two:
if letter == letter2:
letters_in_common.append(letter)
return letters_in_common
returned = common_letters("hello", "loha")
print(returned)
The above code works, but only on the first iteration of the outer loop, hence my letters_in_common list only returns ['h']
when it should return ['h','l','l']
, In JS the syntax is different and the loop works incrementally.
Am I misunderstanding how loops work in Python or is there a syntax level step I have not included?
p.s. I am aware that I could use the 'in' keyword etc but I wanted to try it with a loop first.