I’m attempting to convert this function to a List Comprehension but I have to do a work around using set() to prevent False from repeating. And the locations are not in order. e.g. {0, 12, 5} instead of [0, 5, 12]. Can someone advise where I’m going wrong?
def song_lyrics(a_list, lyric):
location = []
for pos, word in enumerate(a_list):
if word == lyric:
location.append(pos)
elif lyric not in a_list:
return False
return location
the_wall = 'we dont need no education we dont need no thought control no we dont'.split()
result = song_lyrics(the_wall, 'nowe')
print(result)
def song_lyrics(a_list, lyric):
location = set([pos if word == lyric else False for pos, word in enumerate(a_list)])
return location