1

I want to check each list element of c in alphabet and print the letters of the alphabet, which are not in a list element of c.

For example, shall the first list element of c "aa" print all letters of alphabet in a string excluding the letter a.

alphabet = "abcdefghijklmnopqrstuvwxyz"   
c = ['aa', 'bb', 'zz']

for x in c:
  if x in alphabet:
    print(alphabet)
  else:
    print('not an element of alphabet')
3
  • you're missing the expected output... Commented May 27, 2020 at 15:13
  • Do you mean for for aa it should print bcdefghijklmnopqrstuvwxyz and for bb the string acdefghijklmnopqrstuvwxyz? Commented May 27, 2020 at 15:14
  • Yes. Code Pope got the intention of the output. Commented May 27, 2020 at 15:19

3 Answers 3

2

Something like that:

alphabet = "abcdefghijklmnopqrstuvwxyz"   
cases = ['aa', 'bb', 'zz']

for case in cases:
    missing_letters = []
    for letter in alphabet:
        if letter not in case:
            missing_letters.append(letter)
    print(f"Case {case} misses following alphabeth letters {missing_letters}")

Output:

Case aa misses following alphabeth letters ['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Sign up to request clarification or add additional context in comments.

Comments

2

If you are sure that the elements in c are all in the format of 'xx' like in your sample, then the following is a solution:

alphabet = "abcdefghijklmnopqrstuvwxyz"   
c = ['ad', 'bb', 'zz','ad', 'bt', 'uz']

for x in c:
  new_alph = alphabet
  for char in x:
    new_alph = new_alph.replace(char,'')
  if new_alph == alphabet:
    print('not an element of alphabet')
  else:
    print(new_alph)

Output:

bcefghijklmnopqrstuvwxyz
acdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcefghijklmnopqrstuvwxyz
acdefghijklmnopqrsuvwxyz
abcdefghijklmnopqrstvwxy

Another way is to use translate to make the code more compact:

alphabet = "abcdefghijklmnopqrstuvwxyz"   
c = ['ad', 'bb', 'zz','ad', 'bt', 'uz']

for x in c:
  new_alph = alphabet.translate({ord(char): '' for char in x})
  if new_alph == alphabet:
    print('not an element of alphabet')
  else:
    print(new_alph)

Output:

bcefghijklmnopqrstuvwxyz
acdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxy
bcefghijklmnopqrstuvwxyz
acdefghijklmnopqrsuvwxyz
abcdefghijklmnopqrstvwxy

3 Comments

Looks great! Thanks! and if I have two different letters in one list element: c = ['ad', 'bt', 'uz']
@StephanLeinert Ok, now I understand your goal. I have updated the code. You will get the desired result. The solution will work for any length the elements in c have.
@StephanLeinert I have also added another more compact way to do the job. See the edited version.
0

As long as the strings in c are only 2 chars this will work

alphabet = "abcdefghijklmnopqrstuvwxyz"   
c = ['aa', 'bb', 'zz']

for x in c:
  if x[0] in alphabet or x[1] in alphabet:
    alphabet.replace(x[0], '').replace(x[1], '')
  else:
    print('not an element of alphabet')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.