I am almost done with similarities I think. Can't really figure out what is wrong with my substrings

def substrings(a, b, n):
    """Return substrings of length n in both a and b"""
    x = []
    y = []
    for i in range(len(a) - n + 1):
            lista = set(x.append(a[i:i+n]))
    for i in range(len(b) - n + 1):
            listb = set(y.append(b[i:i+n]))
    return lista & listb

Can someone help me out? My errors are

'NoneType' object is not iterable

local variable 'lista' referenced before assignment.

Thank you.

1 Answer 1


(Assuming this is python ...)

Technically, the problem is in the line

lista = set(x.append(a[i:i+n]))

You append something to a list, and you try to convert the result into a set. However, the result of the append method is None.

To get that code technically working, you should split that line up into one line which appends the character to the list, and one line which turns the list into a set.

Of course you only need to convert the list into a set once ...

The real problem is that you did not choose between using a set or using a list.

If you want to process the strings using a list, than you should only use the list to append the characters to. And at the end of the function, you can convert it into whatever you need to get the end result. So in that case, you might use a set at the end of the function.

If you want to process the strings using a set, than you should add the characters to the set instead of first appending them to a list.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .