Search code examples
pythonstringpython-3.xencryptionvigenere

Creating 2D List using for loop in Python3 for vigenere cipher


So, I'm trying to make a program that can encrypt and decrypt vigenere ciphers in python 3. I'm doing this for practice, but I'm really struggling with the creation of a cipher matrix. If you're unfamiliar with vigenere ciphers, here's a helpful photo of what I want.

The function I have to do the switching of the first term to the last is shift, and it works well. I just need to create the lists with every value of the alphabet shifted over. My code is below.

import string

alpha = list(string.ascii_lowercase)


def shift(inList):  #Shifts the first item in the list to the end
    first = inList[0]
    inList.pop(0)
    inList.append(first)
    return inList

lastList = alpha
cipher = alpha
for item in alpha:
    print(alpha.index(item))
    cipher = cipher[].append([shift(lastList)])
    #global lastList = cipher[-1]
    print(lastList)
    print(cipher)

My problem is the creation of the 2D array that holds the vigenere cipher. I can't seem to make it work. Above is the furthest I've gotten making it, and this solution won't compile.


Solution

  • If all you want is creating the table, you can achieve it in one go like this:

    for i in range(26):
         left = string.ascii_uppercase[i:]
         right = string.ascii_uppercase[:i]
         print('{}{}'.format(left, right))