Search code examples
pythonlistslicevigenere

Appending an item to a list with out the [ ] brackets


Im trying to code a Vigenere cipher. I'm building a 2D list that goes

[[a,b,c,d], [b,c,d,a], [c,d,a,b], [d,a,b,c]]

I have it working except the part im slicing from the front and moving to the back ends up with [ ] brackets. eg.

[[a,b,c,d], [b,c,d,[a]], [c,d,[a,b]], [d,[a,b,c]]

my code:

Vigenere Cipher

alphabet = ['a','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']
vigenere = []

for letter in alphabet:
    if letter == 'a': #ie if its the start, no need for anything fancy
        vigenere.append(alphabet[alphabet.index(letter):len(alphabet)])
    else:
        slicer = alphabet[alphabet.index(letter):len(alphabet)]
        slicer.append(alphabet[0:alphabet.index(letter)])
        vigenere.append(slicer)

print(vigenere)

Solution

  • In your else block, slicer.append(alphabet[0:alpha.index(letter)]) will append a list within the slicer list, which is where you run into your problem. Use slicer.extend(...) instead.