Oh, I see what you're trying to do - slice the string. Wrong syntax for that.
dictionary[i:i+3]=[i]
Should be
dictionary[string[i:i+3]]= [i]
Likewise if dictionary[i:i+3] in dictionary:
becomes if string[i:i+3] in dictionary:
and so forth on the next line.
Honestly the way you're doing this makes the code very confusing. Things are clearer if you use setdefault
(and fix a couple of other bugs):
def hash_sequence(s, k=3):
dictionary={}
for i in range(len(s) - k + 1):
triplet = s[i:i+k]
dictionary.setdefault(triplet,[]).append(i)
return dictionary
hash_sequence('byebyeboy')
Out[28]: {'boy': [6], 'bye': [0, 3], 'ebo': [5], 'eby': [2], 'yeb': [1, 4]}