0

I'm trying to find if a string is a Pangram. My approach is to get the string to have unique letters by using the set method. Then using the string.ascii as the base alphabet. I find out after some testing that if I try to compare the 2 with the 'in' operator. Some of the letters get passed over and won't be removed from the alphabet list.

def is_pangram(sentence):
    uniqueLetters = set(sentence.lower().replace(" ", ""))
    alphabet = list(string.ascii_lowercase)

    for letter in alphabet:
      if letter in uniqueLetters:
        alphabet.remove(letter)

    if len(alphabet) <= 0:
      return True

    return False

print(is_pangram("qwertyuiopasdfghjklzxcvbnm"))

this example will compare 13 letters and the rest will not. Anyone can point me in the right direction? Am I misunderstanding something about set?

Jae42
  • 17
  • 2
  • 1
    Don't change the length of a list while iterating over it... – jonrsharpe Mar 12 '19 at 17:20
  • When you have set `uniqueLetters` then convert the `string.ascii_lowercase` to set and apply set operation. It will be fast and should give desired results as per your code. Your current code is checking only 13 characters as you have removed the 13 characters in previous iterations – mad_ Mar 12 '19 at 17:24
  • You can also do this: `alphabet = set(string.ascii_lowercase)` `return uniqueLetters == alphabet` – morsecodist Mar 12 '19 at 17:25
  • This isn't a duplicate... the question isn't asking about 'removing items from a list while iterating', it's asking about how to compare two lists/sets. – monkut Mar 12 '19 at 18:08

1 Answers1

2

Perhaps the following does what you want:

import string
target = set('qwertyuiopasdfghjklzxcvbnm')
all((k in target for k in set(string.ascii_lowercase)))
scient1st
  • 81
  • 5