Q:

Getting all strings that are one character off?

If I have a word 'hungry' and I have a list like ['asdfd', 'hingry', 'hungre', ' hangrr']
How can I get 'hingry' and 'hungre' since they are a character off? I thought about using regex, but I'm not so experienced in using it in python

A:

You can use itertools.zip_longest and sum:
from itertools import zip_longest as _zip
d = ['asdfd', 'hingry', 'hungre', ' hangrr']
word = 'hungry' 
results = [i for i in d if sum(a == b for a, b in _zip(i, word)) >= len(word)-1]

Output:
['hingry', 'hungre']

A:

Once you install python-levenshtein, it's easy as:
>>> from Levenshtein import distance
>>> distance( 'hingry', 'hungry')
1
>>> distance( 'hungre', 'hungry')
1
>>> distance( 'hungr', 'hungry')
1

It automatically handles:

added characters
removed characters
replaced characters

The final code might look like this:
>>> words = ['asdfd', 'hingry', 'hungre', ' hangrr']
>>> [w for w in words if distance( 'hungry', w) < 2]
['hingry', 'hungre']

