For some reason people often ask how to do this without defaultdict
>>> text= "I say what I mean. I mean what I say. i do."
>>> sentences = text.lower().split('.')
>>> dic = {}
>>> for i, sen in enumerate(sentences):
... for word in sen.split():
... if word not in dic: # you just need these
... dic[word] = set() # two extra lines
... dic[word].add(i)
...
>>> dic
{'i': set([0, 1, 2]), 'do': set([2]), 'say': set([0, 1]), 'what': set([0, 1]), 'mean': set([0, 1])}
If you really want lists, here is one modification to do it
>>> text= "I say what I mean. I mean what I say. i do."
>>> sentences = text.lower().split('.')
>>> dic = {}
>>> for i, sen in enumerate(sentences):
... for word in sen.split():
... if word not in dic:
... dic[word] = [i]
... elif dic[word][-1] != i: # this prevents duplicate entries
... dic[word].append(i)
...
>>> dic
{'i': [0, 1, 2], 'do': [2], 'say': [0, 1], 'what': [0, 1], 'mean': [0, 1]}
If you're not even allowed to use enumerate
>>> text= "I say what I mean. I mean what I say. i do."
>>> sentences = text.lower().split('.')
>>> dic = {}
>>> i = -1
>>> for sen in sentences:
... i += 1
... for word in sen.split():
... if word not in dic:
... dic[word] = [i]
... elif dic[word][-1] != i: # this prevents duplicate entries
... dic[word].append(i)
...
>>> dic
{'i': [0, 1, 2], 'do': [2], 'say': [0, 1], 'what': [0, 1], 'mean': [0, 1]}