Hello.
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:

``[b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b]``

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?
Thanks,

Favolas

[b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b]

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 …

Favolas

Try this ok.??

``````sd=[]
sd.append("b"*2)
for x in range(3):
sd.append("a"*5)
sd.append("b"*4)
df=sd[0:-1]
df.append("b"*2)
print(df)

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

Hope it does work

## All 5 Replies

[b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b,b,b,a,a,a,a,a,b,b]

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.

Hello.
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.

Hello.
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):
teste.insert(a,list_b)
j = j + 1``````

Favolas

Try this ok.??

``````sd=[]
sd.append("b"*2)
for x in range(3):
sd.append("a"*5)
sd.append("b"*4)
df=sd[0:-1]
df.append("b"*2)
print(df)

## 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):
list_in.append(list_a[0])
del list_a[0]
return list_in, list_a

for x in range(n):
list_in.extend(list_b)
return list_in

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

next_list = []

## append first n=2

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):