Question: sorry for asking but I'm kind of new to these things. I'm doing a splitting words from the text and putting them to dict creating an index for each token:

```
import re
f = open('/Users/Half_Pint_Boy/Desktop/sentenses.txt', 'r')

a=0
c=0
e=[]

for line in f:

    b=re.split('[^a-z]', line.lower()) 
    a+=len(list(filter(None, b)))
    c = c + 1
    e = e + b

d = dict(zip(e, range(len(e))))

```

But in the end I receive a dict with spaces in it like that:

```
{'': 633,
 'a': 617,
 'according': 385,
 'adjacent': 237,
 'allow': 429,
 'allows': 459}

```

How can I remove "" from the final result in dict? Also how can I change the indexing after that to not use "" in index counting? (with "" the index count is 633, without-248)
Big thanks!

Answer: 
How about this?

```
b = list(filter(None, re.split('[^a-z]', line.lower())))

```

As an alternative:

```
b = re.findall('[a-z]+', line.lower())

```

Either way, you can then also remove that `filter` from the next line:

```
a += len(b)

```

**EDIT**

As an aside, I think what you end up with here is a dictionary mapping words to the **last position in which they appear in the text**. I'm not sure if that's what you intended to do. E.g.

```
>>> dict(zip(['hello', 'world', 'hello', 'again'], range(4)))
{'world': 1, 'hello': 2, 'again': 3}

```

If you instead want to keep track of *all* the positions a word occurs, perhaps try this code instead:

```
from collections import defaultdict
import re

indexes = defaultdict(list)

with open('test.txt', 'r') as f:
    for index, word in enumerate(re.findall(r'[a-z]+', f.read().lower())):
        indexes[word].append(index)

```

`indexes` then maps each word to a list of indexes at which the word appears.

**EDIT 2**

Based on the comment discussion below, I think you want something more like this:

```
from collections import defaultdict
import re

word_positions = {}

with open('test.txt', 'r') as f:
    index = 0
    for word in re.findall(r'[a-z]+', f.read().lower()):
        if word not in word_positions:
            word_positions[word] = index
            index += 1

print(word_positions)

# Output:
# {'hello': 0, 'goodbye': 2, 'world': 1}

```
Your regex looks not a good one. Consider to use:

```
line = re.sub('[^a-z]*$', '', line.strip())
b = re.split('[^a-z]+', line.lower())

```