Home > other >  increment of list elements in a certain pattern
increment of list elements in a certain pattern

Time:06-03

I have a list for example: [1,2,3,4] I want to have ten repeats of the list, and every element increase by 1 than the previous element like: [1,2,3,4,2,3,4,5,3,4,5,6................40 elements]

CodePudding user response:

temp = [1, 2, 3, 4]
lst = [i   j for i in range(9) for j in temp]
print(lst)

# which equals to this:
temp = [1, 2, 3, 4]
lst2 = []
for i in range(9):
    for j in temp:
        lst2.append(i   j)
print(lst2)

CodePudding user response:

plz have a look. I don't think it goes upto [1,2,..40] , only goes upto[1,2,.. 14].

At each iteration you are increasing the value by 1 so, after 10 times repeating every no in the array will be increased by 10. So 4 will be 14 which will be the last element not 40. Here's my code according to your requirement,

arr = [1,2,3,4]
answer = [ ]
for i in range(11):
    for element in arr:
        answer.append(element   i)
print(answer)
  • Related