User1
User1

Reputation: 19

What is a good way to extend a list with a repeated number?

I want to extend a list with the number 2450, 50 times. What is a good way to do this?

for e in range(0,50):
    L2.extend(2450)

Upvotes: 0

Views: 83

Answers (1)

HennyH
HennyH

Reputation: 7944

This will add 2450, 50 times. l2.extend([2450]*50)

Example:

>>> l2 = []
>>> l2.extend([2450]*10)
>>> l2
[2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450]

Or better yet:

>>> from itertools import repeat
>>> l2 = []
>>> l2.extend(repeat(2450,10))
>>> l2
[2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450, 2450]

Upvotes: 9

Related Questions