Multiply or duplicate number in a list. Python

Viewed 28

How can I duplicate myData. For example

myLen = [1,2,3,4,5,6]

duplicate the number by the length of the 'myLen' and also convert the string to number

myData = ['2']

output:

[2,2,2,2,2,2]
2 Answers

try:

list(map(int,myData*len(myLen)))

What you could do is selecting the index of the element you need in myLen and then apply it to my data:

i = 0    
n = myLen[i]
myData = ['2']**n

Of course you could decide what position you need by changing the value of i but make sure it's not greater than the length of myLen otherwise you'll get an error.

Basically the syntax [x]**n creates a list of length n with element x in it.

Related