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: 
Your code is almost right! The problem is that `mydict[lemma] = word` is going to *overwrite* any existing `word`s associated with that `lemma`. In reality each lemma can have more than one word associated with it , so what we really want to do is add the new word to a collection, rather than overwrite the existing value. The natural way to model this is with a dict of *lists* of strings, rather than a dict of strings.

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

```

What does that `if` statement mean? If there is already an entry for a given `lemma` in `mydict`, then we add it to the list. If it's not already there, we initialise the entry with a singleton list.

I've also taken the liberty of using Python's syntax for unpacking sequences. `word, lemma = line.split()` is equivalent to `word = line.split()[0]; lemma = line.split()[1]`.

One of the nice things about Python is that many simple tasks are catered for by the standard library. This sort of use-case is just what the standard library's [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) was designed for. When you index into a `defaultdict` using the subscripting operator (`dict[x]`), if no value is found it is created using the function you supply. Here, we tell the `defaultdict` to initialise a new list.

```
from collections import defaultdict
mydict = defaultdict(list)

```

Now the code is simpler because `defaultdict` is taking care of checking `if lemma in mydict` for us. It'll always give us back a (possibly-empty) list to which we can add the word.

```
for line in myfile.splitlines():
    word, lemma = line.split()
    mydict[lemma].append(word)

```

In a comment, you suggested that you didn't want duplicate `word`s in the output. The standard library can help here, too. [`set`](https://docs.python.org/3/library/functions.html#func-set) is a data structure a bit like a list, except that it doesn't remember the order of the elements, and it automatically removes duplicates. (Under the hood, it works like a `dict` with only keys and no values.)

We just need to change the `defaultdict` function from `list` to `set`, and change the `append` method to the version which works on `set`s (which is called `add`.)

```
from collections import defaultdict
mydict = defaultdict(set)

for line in myfile.splitlines():
    word, lemma = line.split()
    mydict[lemma].add(word)

```
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]

```