Question: I have a text file in the format

```
word lemma
word lemma
word lemma    

```

like this

```
workers  work
went     go
going    go
work     work
working  work

```

What I have to do is to collect in dict the lemma with its words, so the output will like this

```
{work: workers, work, working}
{go: went, going}

```

I tried this

```
mydict = {}
for line in myfile.splitlines():
    lemma = line.split()[1]
    word = line.split()[0]
    mydict[lemma] = word

```

But this code gives the following output:

```
{'work': 'working', 'go': 'going'}

```

Can someone help?

Answer: 
as my comment suggests, you need to make a list as your value, not just setting the value for each key. somethign like this should work.

```
mydict = {}
for line in myfile.splitlines():
    lemma = line.split()[1]
    word = line.split()[0]
    if lemma in mydict.keys():
        mydict[lemma].append(word)
    else:
        mydict[lemma] = [word]

```
If you're interested in the ~~insane~~ functional approach to this, there is a "single logical line" way to get it done, that doesn't involve creating an empty dictionary and avoids the repeated calls to `append`

```
import itertools
import operator

with open('myfile.txt') as myfile:
    mydict = dict((k, [p[1] for p in grp]) 
         for k, grp in itertools.groupby(
             sorted(list(reversed(ln.split())) for ln in myfile),
             operator.itemgetter(0)))

```

which says
take each line of the file, split it, then [reverse](https://docs.python.org/3/library/functions.html#reversed) it  

[sort](https://docs.python.org/3/library/functions.html#sorted) those lines  

[group](https://docs.python.org/3/library/itertools.html#itertools.groupby) them by their element[`[0]`](https://docs.python.org/2/library/operator.html#operator.itemgetter)  

map the element `[0]` to each word in the group by taking the `[1]` of the reversed split line.