set with len

Pangram in Python
def is_pangram(sentence):
    return len([ltr for ltr in set(sentence.lower()) if ltr.isalpha()]) \
        == 26

  • This approach first makes a set from the lowercased characters of the sentence.
  • The characters in the setare then iterated in a list comprehension.
  • The characters are filtered by an if isalpha() statement, so that only alphabetic characters make it into the list.
  • The function returns whether the len() of the list is 26. If the number of unique letters in the set is equal to the 26 letters in the alphabet, then the function will return True.
22nd May 2024 · Found it useful?