Python arranging list with for loop

30 views Asked by At

The question along with the solution is posted. But i didn't understand the solution, especially inside the for loop. Can someone elaborate the code for me?

[1]: https://i.stack.imgur.com/p1Lkp.jpg![enter image description here](https://i.stack.imgur.com/dMYgZ.jpg)

1

There are 1 answers

0
Daniel Hao On

here I try to put in some explanations for the problem you posted.
Actually, it's easier if you can run it through the platform to see the program in action - http://pythontutor.com/ next time.

# interleave a list items:
#

array = [3, 6, 1, 2, 'a', 'c', 'b', 'z']
#        0  1  2  3   4    5    6   7

N = len(array)    # the length of the array a

out =  []         # to stort the output

c = mid = N //2   # mid-point of the array
print(f' c: {c} ')  

for item in array:
    if c != N:
        print(array[c])
        
        out += [item] + [array[c]]   # get 4th item - 'a', append it; then continue

        c += 1   # moving this mid-point
        print(f' c is {c} ')

print(out)