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: 
Well I would suggest using a `defaultdict`, initialized with a `list` from the `collections` module. It fits this case since you can easily add new elements and have the `list` ready for adding elements. 

Creating a `defaultdict` is easily done by importing it from collections and providing a `list` as the sole argument:

```
from collections import defaultdict

mydict = defaultdict(list)

```

Then, you can iterate for every line in your file and `append()` the `word` according to the value of `lemma` :

```
with open('lemmaaa', 'r') as myfile:
    for line in myfile:
        lemma = line.split()[1]
        word = line.split()[0]
        mydict[lemma].append(word)

```

Using the input you supplied, `print(mydict)` outputs the mapping you're looking for: 

```
defaultdict(<type 'list'>, {'go': ['went', 'going'], 'work': ['workers', 'work', 'working']})

```
```
import csv
import collections

with open('path/to/file') as infile:
    answer = collections.defaultdict(list)
    for word, lemma in csv.reader(infile, delimiter='\t'):
        answer[lemma].append(word)

```

If you want to do this without any help from csv:

```
answer = {}
with open('path/to/file') as infile:
    for line in infile:
        word, lemma = line.split()
        if lemma not in answer: answer[lemma] = []
        answer[lemma].append(word)

```