0

I have data in a list: ls1 which prints fine when giving the print function

[5, 2, 7, 4, 3, 9, 8, 6, 10]

However, I am getting an error when trying this:

P81=[]  
P81.append(ls1[5])

Is there anything wrong with the code? Here is a full copy for reference. The code is just a key generation function which accepts a 10 element list and performs some permutations and shifts. leftShift is a function which just performs a shift operation on the list.

def keyGen(key):
    import numpy
    #3 5 2 7 4 10 1 9 8 6
    P10=[]
    P10.append(key[2])
    P10.append(key[4])
    P10.append(key[1])
    P10.append(key[6])
    P10.append(key[3])
    P10.append(key[9])
    P10.append(key[8])
    P10.append(key[7])
    P10.append(key[5])

    #Now, P10 contains the keys after initial permutation

    #Take 2 halves and perform left shift
    ls1a=leftShift(P10[0:5])
    ls1b=leftShift(P10[5:10])
    ls1=ls1a+ls1b

    P81=[]
    #6 3 7 4 8 5 10 9

    print ls1
    P81.append(ls1[5])
    P81.append(ls1[2])
    P81.append(ls1[6])
    P81.append(ls1[3])
    P81.append(ls1[7])
    P81.append(ls1[4])
    P81.append(ls1[9])
    P81.append(ls1[8])

    #For the second set of keys perform the second shift
    ls2a=leftShift(ls1a)
    ls2b=leftShift(ls1b)
    ls2=ls2a+ls2b

    P82=[]
    P82.append(ls2[5])
    P82.append(ls2[2])
    P82.append(ls2[6])
    P82.append(ls2[3])
    P82.append(ls2[7])
    P82.append(ls2[4])
    P82.append(ls2[9])
    P82.append(ls2[8])

    return([P81,P82])

1 Answer 1

2

The index error is for indexing into ls1, not the .append() call.

Your ls1 does not have 10 elements, yet you try to index that many:

P81.append(ls2[9])
P81.append(ls2[8])

You only ever appended 9 elements to P10 (you ignored key[0]), so your assumptions already fall apart there. As a result, provided leftShift doesn't lose any more elements, ls1 is 9 elements long, so:

P81.append(ls1[9])

will fail. Even if it doesn't, you ignore ls1[0] and ls1[1]. ls2 suffers from the same problem; there are 9 elements in that list, not 10, provided leftShift doesn't drop any elements.

Sign up to request clarification or add additional context in comments.

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.