I'm new in Python and I only want to use basic programing skills.

I have this example code:

list_a = [a, ] * 15
list_b = [b,]
s = 5
n = 2

and want to obtain this:


basically I want to insert list_b the number of times said in n between list_a defined the number of times in s.

How can I do this?



You insert 2, then 4, then 4, then 2? Please correct this example or be more specific about what you want.

Instead of inserting, it is more efficient to create a new list since every item past the insertion point has to be moved for every insert. You can use 2 for() loops; the first would have an increment or step value of s. The second or inner loop would append n values of 'b'. The outer loop would then append s values of 'a' before looping again. If the length of the original list is not always divisible by s, then you would have to test for less than length before appending an 'a' This could probably be done with list comprehension, but many do not consider it a basic skill. Try out some double for() loop code for yourself and post back with questions on that code as it is physically impossible for the few of us who answer to provide code for everyone who asks.

Yes. In list_a I want to insert 2 times the list_b before the first a, then count five a's and insert 2 times the list_b. Insert again 2 times list_b, 5 a's and 2 times list_b. And so on... 2 - 5 - 2 - 2 - 5 - 2 - ...

One other method is for example, one loop that inserts, after 5 a's 4 b's and so on. Then, insert 2 b's in the beginning and other 2 in the end.

I'm doing this code but it does not work.

j = 0
for i in range(3):
        for a in range(0,len(list_a),11):
                j = j + 1


Try this ok.??

for x in range(3):  

## output ##
['bb', 'aaaaa', 'bbbb', 'aaaaa', 'bbbb', 'aaaaa', 'bb']

Hope it does work

This is my opinion of what you are asking, but a better example would help:

def add_a(list_in, list_a, s):
    for x in range(s):
        if len(list_in):
            del list_a[0]        
    return list_in, list_a

def add_b(list_in, list_b, n):
    for x in range(n):
    return list_in

a = 'a'
b = 'b'
list_a = [a, ] * 15
list_b = [b,]
s = 5
n = 2

next_list = []

## append first n=2
next_list = add_b(next_list, list_b, n)

while len(list_a):
    ## append "s" number of "a"
    next_list, list_a = add_a(next_list, list_a, s)

    ## append n*n list_b
    if len(list_a):
        for x in range(n):
            next_list = add_b(next_list, list_b, n)

## append last n=2
next_list = add_b(next_list, list_b, n)
print next_list