Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating list in python N times? [duplicate]

Tags:

python

I have a list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and I want to repeat a list n times.
For example, if n = 2 I want [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as output.

Is there any inbuilt solution in python except append and for loop because value of n might go up to 1000 too ?

like image 969
Paras Ghai Avatar asked Oct 31 '25 09:10

Paras Ghai


1 Answers

Python allows multiplication of lists:

my_list = [0,1,2,3,4,5,6,7,8,9]
n = 2
print(my_list*n)

OUTPUT:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

like image 165
Gsk Avatar answered Nov 02 '25 00:11

Gsk