def legal_onsets(self, syllables):
        """
        Filters syllable respecting the legality principle
        :param syllables: str list

        Example:
            The method scans for invalid syllable onsets:

            >>> s = Syllabifier(["i", "u", "y"], ["o", "ø", "e"], ["a"], ["r"], ["l"], ["m", "n"], ["f", "v", "s", "h"], ["k", "g", "b", "p", "t", "d"])

            >>> s.set_invalid_onsets(['lm'])

            >>> s.legal_onsets(['a', 'lma', 'tigr'])
            ['al', 'ma', 'tigr']

            You can also define invalid syllable ultima:

            >>> s.set_invalid_ultima(['gr'])

            >>> s.legal_onsets(['al', 'ma', 'ti', 'gr'])
            ['al', 'ma', 'tigr']

        """

        vowels = self.vowels

        for i in range(1, len(syllables)):
            onset = ""
            
            for letter in syllables[i]:

                if letter in vowels:
                    break

                onset += letter

            for j in range(len(onset)):
                # Check whether the given onset is valid
                if onset[j:] not in self.invalid_onsets:
                    syllables[i - 1] += onset[:j]
                    syllables[i] = syllables[i][j:]
                    break

        # Check whether ultima is invalid

        if syllables[-1] in self.invalid_ultima:
            syllables[-2] += syllables[-1]
            syllables = syllables[:-1]

        return syllables