0

I am new to python. I think n begins at 0 and it will increase up till the last element on stack1.

arraylength = 3*stack1length
array = [0]*arraylength
for n in stack1:
    array[3*n] = stack1[n]

my array length is 3 times the length of stack1

1 Answer 1

9
for n in stack1:

Goes through the items in stack1.

You seem to be wanting to go through the indexes:

for n in range(len(stack1)):
    array[3*n] = stack1[n]

Note that this is better written with the convenience function, enumerate,

for n, stack1_n in enumerate(stack1):
    array[3*n] = stack1_n

Additionally, you can use some evil hackz:

array[::3] = stack1

array[::3] is every third item in array (start:stop:step), and therefore you're setting every third item to the corresponding item in stack1.

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

2 Comments

I don't think that's evil hacks. I would consider that Pythonic. haha
Haven't you heard? Python is an evil hack. A sweet, juicy hack upon hacks. ;)

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.