Q:

Trying to return a string for each number of same characters that share the same index in two strings

I wrote the following code myself (based on some instructions explained in the doc string):
def find_correct(answer, guess):
'''
(list, list) -> list
Given the answer code and a guess code, return
a list containing the string 'b' for each correctly positioned
colour in the guess. For example, if answer is 'YOGB' and guess is
'RTLB', ['b'] will be returned since index 3 in both is B. If answer is 'YOGB'
and guess is 'YTLB', ['b','b'] will be returned since index 0 in both is Y and
index 3 in both is B (ie. two 'b's in the list since two indexes match)
'''
for i in range(len(guess)):
    if answer[i] == guess[i]:
        return ['b']

However this only returns one ['b'] no matter how many indexes match (unless none match in which case nothing is returned), since I just wrote return ['b']. I don't know how to make it return more than one b. I tried a few things like:
 return ['b'] for i in range(len(guess)):
     if answer[i] == guess[i]

And doing
  return ['b'] * i

But obviously none of these work for reasons I think I understand (but I thought I'd try anyway). Is there an easy way to make the code do what I want without completely re-writing the code I already wrote or should I go back to the drawing board? Maybe perhaps there is some way i could use the a.count(b) function then multiply that result by ['b']? Just an idea I've been trying to work with. Thanks!

A:

May be you want this :
def find_correct(answer, guess):
    l=[]
    for i in range(len(guess)):
        if answer[i] == guess[i]:
            l.append('b')
    return l
print(find_correct('YOGB','RYLB'))

Output:
['b']

Remember that whenever return is encountered, you come out of the function. So, make a list and return it at the end.
If you want a one liner, 
def find_correct(answer, guess):
    return ['b' for i in range(len(guess)) if answer[i] == guess[i]]

