I am trying to write a function which consumes a string and a character and produces a list of indices for all occurrences of that character in that string.
So far this is what I have, but it always gives me [].
def list_of_indices(s,char):
string_lowercase = s.lower()
sorted_string = "".join(sorted(string_lowercase))
char_list = list(sorted_string)
for x in char_list:
a = []
if x == char:
a.append(char_list.index(x))
return a
I don't understand why this does not yield the answer. And it has to be a list of non-empty length.
Anyone aware of how to get the indices for all occurrences?