+ 1
Is there a way to repeat a sequence of numbers for a set amount?
For example, list = [1,2,3,4,5] I want to repeat the items in the list 7 times like so (1,2,3,4,5,1,2). Is this possible?
6 Answers
+ 5
from itertools import cycle
def repeat(lst, times):
c = cycle(lst)
for i in range(times):
yield next(c)
print(*repeat([1,2,3,4,5], 7))
+ 4
numbers = [1, 2, 3, 4, 5]
for i in range(7):
print (numbers[0])
numbers.append (numbers.pop(0))
+ 3
from itertools import cycle
numbers = cycle([1, 2, 3, 4, 5])
for i in range(7):
print (next(numbers))
+ 2
Jan Markus Well, that's how I understood the question.
How about this then?
a = [1,2,3,4,5]
n = 7
print((a * (n // len(a) + 1))[:n])
#[1,2,3,4,5,1,2]
0
list+list[:7-len(list)]?
- 1
What are you trying to do? The easiest way would be
a=[1,2,3,4,5]
print(7 * a)
You can also use also use this to iterate over the list:
for n in 7 * a:
print(n)