Help searching for two elements in a list

I need to search a list of text for the instance of ‘pepper’ and see if ‘spray’ follows, and then combine the two, and the same for the instance of ‘machine’ and see if ‘gun’ follows, and combine as well utilizing a loop.

What have you tried?

I’ve tired running a for loop over the length of the list, and then using an index function to find the value of the word “machine” or “pepper”, and then adding one to the index of those words and forming an if statement to see if they == “gun” or “spray”, however it hasn’t worked so far.

Two pointers:

for next_idx, value in enumerate(arr[:-1], 1):  # or range(len(arr)-1) if you need flexibility
    if value == "pepper" and arr[next_idx] == "spray":

I got the two pointer method to work, however how do you combine two indexes and insert them back into the list at the original position? For instance, if ‘machine’ had the index of [value] and ‘gun’ had the instance of [next_idx], how would I combine them into ‘machinegun’ at index [value] and delete ‘gun’ at index [next_idx]?

I’m pretty new to Python and I appreciate all the help!

are arr[value] and arr[next_idx] always right next to each other? if so, what if you access arr[value+1] instead of arr[next_idx]? if so, what i would do is arr[value:value+2] = arr[value]+arr[value+1]. there could be an even better way to do that, for various kinds of better.

the magic is that “:” in there. this is slicing. it is a very powerful feature of Python you may not have been introduced to. in this case it would let you replace two adjacent items in a list with a single item (expressed as in a list). if you are that kind of person, read ahead about it then play around with it.

Slight bug there. It would be arr[value:value+2] = [arr[value] + arr[value+1]].

What I would do is: create an empty list, iterate over the index of a list with two pointers, append words to a new list, and in case of a match - join words, add them together and skip over the pointer by one (e.g. to avoid “pepper spray”, “spray” scenario).

you are correct. but a slight bug is still a bug.

perhaps you could do that and get the correct result. but it is slow to do it that way, compared to what can be done. what if the list is a million total elements? what if this needs to be repeated ten thousand times?

What you can do in these cases is use an alternative implementation of Python called PyPy which dramatically speeds up highly repetitive for loops (so long as your code does not rely on libraries that use C extensions) - Just In Time (JIT) Compilers - Computerphile - YouTube

Iterating over a list with two pointers is O(n), which is not too bad. I’d be surprised if there was a clever algorithm that could do better, although you probably can write a clever function in NumPy using np.roll.

I believe I would take the original list and create a dict from it, where the keys are the index in the original list, and the values are the words from the original list.

You could then do this in O(n) time.

Although assigning to a slice has an excellent constant, it’s O(n) itself, giving an O(n^2) algorithm.

Naturally, O(n) beats O(n^2) when you need short running times.

1 Like