I wrote this recursive function to check for a specific string in a list and if the string is found, it should be returned reversed within the list but I can't seem to keep the list as a list. my output is a joined string. can someone please suggest how to maintain the list type?
test_list = ['cat', 'hat', 'bat', 'sat']
test_string = 'bat'
def reverser(some_list, some_string):
if some_list == []:
return ''
elif len(some_list) == 1 and item in some_list == some_string:
return some_string[::-1]
else:
if some_list[0] == some_string:
return some_string[::-1] + reverser(some_list[1:], some_string)
if some_list[0] != some_string:
return some_list[0] + reverser(some_list[1:], some_string)
reverser(test_list, test_string)
output is:
'cathattabsat'
but i would like it to be:
['cat', 'hat', 'tab', 'sat']