You should change res=res.append(finallist)
into res.append(finallist)
Because res.append(finallist)
will not return a 'list' object, but just the success of that line of code, which is a 'NoneType' object. (actually it returns nothing.)
Now your code will look like this:
def searchlist(lst, letter):
res = []
for i in range(len(lst)):
finallist = lst.index(letter)
res.append(finallist)
return res
However, even though you run this code, your program will not give you a result what you want, e.g. lst=['a', 'b', 'c']
if letter='a'
then [0, 0, 0]
.
You'd better use the code below to get a result what you want.
def searchlist(lst, letter):
res = []
for i, element in enumerate(lst):
if element == letter:
res.append(i)
return res
test_result = searchlist(['a', 'b', 'c', 'a'], 'a')
print(test_result)
Or you can make your code much more simple and cooler (which is preferable) by this way:
def searchlist(lst, letter):
return [i for i, element in enumerate(lst) if element == letter]
test_result = searchlist(['a', 'b', 'c', 'a'], 'a')
print(test_result)
We call this technique as "list comprehension"
I got this idea from https://stackoverflow.com/a/16685428/8423105
Good luck.