Sequence preprocessing function. Assuming sequence is a Python list. Tested a bit.