Searching for this on generic_search_machine returns nothing helpful, except a lot of beginner tutorials trying to teach me what a list is.

Background:
This code tries to find all words from

words_list

which match

pattern

.

pattern

is a list of characters a user entered by swiping over the keyboard, like on most modern mobile device's keyboards.

In the actual implementation there is the additional requirement that the returned word should be longer than 5 characters and the first and last character have to exactly match. These lines are omitted here for brevity, since they are trivial to implement.

Even without looking at the source code, you can always assume that the underlying implementation is smart enough to implement that efficiently, i.e. that it does not look at the first i values in the list.

As a rule of thumb, using a built-in functionality is always faster than (or at least as fast as) the best thing you can implement yourself.

The attempt to make it better:

i += pattern[i:].index(character)

This is deffinitely worse. It makes a copy ofpattern[i:] and then looks for character in it.

So, in the worst case, if you have a pattern of 1 GB and i=1, this copies 1 GB of data in memory in attempt to skip the first element (which whould have been skipped anyway).