Search code examples
pythonlistloopsencryptionvigenere

Vigenere Cipher List Processing


So, I'm making my own Vigenere cipher program in Python and I've come as far as having two lists; one is a list with the ASCII numbers of the message to be encrypted/decrypted and the other is the same length and contains the ASCII numbers of the cipher key repeated. To add these two lists together I used this line of code:

encryption =  [x + y for x, y in zip(msglist, keylist)]

This works but I want the loop to ignore ASCII numbers that are not part of the alphabet but I have no idea how I can do this. I tried doing something like:

encryption =  [if chr(x).isalpha() != True: x + y for x, y in zip(msglist, keylist)]

but the syntax is invalid! What do I need to do to make this work?


Solution

  • If you still want to include the digit characters but not encrypted, use the Python variant on a ternary if:

    [x + y if chr(x).isalpha() else x for x, y in zip(msglist, keylist)]
    

    If you want to ignore them entirely, use the if part of list comprehensions:

    [x + y for x, y in zip(msglist, keylist) if chr(x).isalpha()]